ipython notebook を html に吐き出してブログ記事に張り付ける

ipython 便利ですよね.
notebook を使うとさらに便利ですよね.
それをブログ記事に張り付けられたら,楽ちんですよね!

というわけで,調べてみました.

ipython nbconvert --to html GmmMethods.ipynb --template basic

たったこれだけ!
あとは,できたhtmlファイルの内容をコピーして,ブログの記事を書くところに張り付けてあげれば,下記のような感じで表示されます.
ただ,画像とかはやっぱり表示されないんで,そのあたりは何か手を考えないとです.
もっと言えば,コピペすらめんどくさいです.
もっといい方法ないんでしょうか???

 

GMM 学習・尤度計算・シリアライズ etc.

 

共通インポート

In [4]:
#coding:utf-8
from numpy import dtype, fromfile, diag
from sklearn.mixture import GMM
from sklearn.externals import joblib
import pylab as pl
import matplotlib as mpl
from pylab import *
import json, codecs
 

バイナリファイル読み込み

バイナリファイルを読み込んでNumpy.arrayに格納する関数です.
col次元のデータをrowフレーム分読み込みます.
arrayには,array[row][col] とう形で保存されます.

In [1]:
def ReadData(filename, dim = 24, dtype = "<d8"):
    f = open(filename, 'rb')
    data = fromfile(f, dtype)
    row = data.size / dim
    data.resize(row, dim)
    return data
 

GMMをJsonに吐き出す

In [24]:
def Gmm2Json(gmm, jsonFile):
    import json, codecs
    import datetime
    import locale
    
    d = datetime.datetime.today()
    print 'd == %s : %s\n' % (d, type(d))
    
    f = codecs.open(jsonFile, "w", "utf-8")
    json.dump(
        {
            "n_components": gmm.n_components, 
            "covariance_type": gmm.covariance_type, 
            "params": {
                "weight": gmm.weights_.tolist(), 
                "mean": gmm.means_.tolist(), 
                "covariance": model.covars_.tolist(),
            }
        }
        , f, indent = 2, sort_keys = False
    )
 

Test

In [25]:
if __name__ == "__main__":
    
    data = ReadData("../wav/p003.mcep", dim = 24, dtype = "<d8")
    print data.shape
    
    model = GMM(n_components = 16, 
                covariance_type = 'diag', 
                random_state = None, 
                thresh = 1e-2, 
                min_covar = 1e-3, 
                n_iter = 100, 
                n_init = 1, 
                params = 'wmc', 
                init_params = 'wmc')
    model.fit(data)
    
    Gmm2Json(model, "test.json")
    
    #joblib.dump(model, "model.gmm", compress = 0)
    imshow(diag(model.covars_[0]), cmap = 'spectral')
    show()
 
(3645, 24)
d == 2014-08-03 00:38:17.329000 : <type 'datetime.datetime'>


 
 
In []: