金子邦彦研究室プログラミングPythonVBGMM (Variational Bayesian Gaussian Mixture) を用いてクラスタリング(Python, scikit-learn を使用)

VBGMM (Variational Bayesian Gaussian Mixture) を用いてクラスタリング(Python, scikit-learn を使用)

VBGMM では,クラスタ数を指定せずにクラスタリングを行う

前準備

Python の準備(Windows,Ubuntu 上)

サイト内の関連ページ

関連する外部ページ

Python の公式ページ: https://www.python.org/

Python の numpy, pandas, seaborn, matplotlib, scikit-learn のインストール

3. Iris データセットの準備

  1. Iris データセットの読み込み
    import pandas as pd
    import seaborn as sns
    sns.set()
    iris = sns.load_dataset('iris')
    
  2. データの確認
    print(iris.head())
    
  3. 形と次元を確認

    配列(アレイ)の形: サイズは 150 ×5.次元数は 2. 最後の列は,iris.target は花の種類のデータである

    print(iris.shape)
    print(iris.ndim)
    
  4. Iris データセットの0, 1, 2, 3列目を表示
    print( iris.iloc[:,0:4] )
    

    [image]

VBGMM (Variational Bayesian Gaussian Mixture) によるクラスタリング

  1. 散布図に主成分分析プロットの準備
    # 主成分分析
    def prin(A, n):
        pca = sklearn.decomposition.PCA(n_components=n)
        return pca.fit_transform(A)
    
    # 主成分分析で2つの成分を得る
    def prin2(A):
        return prin(A, 2)
    
    # M の最初の2列を,b で色を付けてプロット.b はラベル
    def scatter_label_plot(M, b, alpha):
        a12 = pd.DataFrame( M[:,0:2], columns=['a1', 'a2'] )
        f = pd.factorize(b)
        a12['target'] = f[0]
        g = sns.scatterplot(x='a1', y='a2', hue='target', data=a12, palette=sns.color_palette("hls", np.max(f[0]) + 1), legend="full", alpha=alpha)
        # lenend を書き換え
        labels=f[1]
        for i, label in enumerate(labels):
            g.legend_.get_texts()[i].set_text(label) 
        plt.show()
    
    # 主成分分析プロット
    def pcaplot(A, b, alpha):
        scatter_label_plot(prin2(A), b, alpha)
    
  2. Iris データセットの0, 1, 2, 3列目について、VBGMM (Variational Bayesian Gaussian Mixture) を用いてクラスタリング
    import numpy as np
    from sklearn.mixture import BayesianGaussianMixture
    
    bgmm = BayesianGaussianMixture(n_components=10, verbose=1).fit(iris.iloc[:,0:4]) 
    X = iris.iloc[:,0:4].values
    c = bgmm.predict(X)
    print(c)
    
  3. 結果をプロット

    乱数を使っているので、実行するたびに違った値になる

    pcaplot(X, c, 1)   
    
  4. 比較のため,Iris データセットをプロット
    pcaplot(X, iris.iloc[:,4], 1)