科学の箱

科学・IT・登山の話題

機械学習

irisを教師なし学習で分類ーGMM

投稿日:

これまでは正解ラベルがある前提、つまり教師あり学習でモデルを構築した。今回は正解ラベルがない前提でモデルを構築する。

教師なし学習としては混合ガウスモデルを利用する。

内容

  • データの読み込み、前処理
  • モデルの構築
  • プロット

データの読み込み、前処理

データの読み込み、前処理はこれまでと同様である。

import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
import numpy as np

# データセット読み込み
iris = sns.load_dataset('iris')
iris.head()

# データ前処理
X_iris = iris.drop('species', axis=1)
X_iris.head()

 

モデルの構築

混合ガウスモデルはsklearn.mixture.GaussianMixureを利用する。

from sklearn.mixture import GaussianMixture as gm 
model = gm(n_components=3,covariance_type='full')
model.fit(X_iris)
y_gmm = model.predict(X_iris)
print(y_gmm)
# [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 2 1 2 1 2
 2 2 2 1 2 2 2 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1
 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 1 1]

 

プロット

2次元にプロットするためにはPCAで2次元にする。

from sklearn.decomposition import PCA
pca = PCA(n_components=2)     
pca.fit(X_iris)               
pca_transformed = pca.transform(X_iris)
# データマージ
iris["PCA1"] = pca_transformed[:, 0]
iris["PCA2"] = pca_transformed[:, 1]

 

さらにy_gmmもマージする。

iris["cluster"] = y_gmm

iris.head()

PCA1, 2およびクラスタがすべての一つのデータフレームに収まったのでプロットして検証する。

sns.lmplot("PCA1", "PCA2", hue='species', data=iris, fit_reg=False);
sns.lmplot("PCA1", "PCA2", hue='cluster', data=iris, fit_reg=False);

gmmはほぼ正しく分類できている。seaborn.lmplotはhueのほかにcolを利用してさらにグラフを分割することができる。

 

hue, col, row : strings
Variables that define subsets of the data, which will be drawn on separate facets in the grid. See the *_order parameters to control the order of levels of this variable.

 

sns.lmplot("PCA1", "PCA2", hue='species', col="cluster", data=iris, fit_reg=False);

メタ情報

inarticle



メタ情報

inarticle



-機械学習
-

執筆者:


comment

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

関連記事

no image

Pythonではじめる機械学習 – k-Means

k-Meansの概要 k-Meansは教師なし学習の一つであり、あらかじめ指定された数にグループを分ける。 例えば今身長である程度まとまって席に座っている生徒を考えます。k-Meansでは最初に分ける …

no image

DictVectorizerを使ってカテゴリデータについて特徴抽出をする

都市における温度データを考える。 measure = [ {‘city’: ‘Dubai’, ‘temperature’: 33.}, {‘city’: ‘London’, ‘temperature’ …

no image

データ分析の基本と業務

開発生産性は調和平均で計算する。 安全性の在庫の計算には正規分布を利用できる。 標準偏差=5であることを利用すると何が言えるか。 Z=1.96で95%をカバーする。 よって 5 x 1.96でほぼ10 …

no image

データ分析について評価する

デザイン コントロールグループはあるか モデル 検定の前提は満たしているか(正規分布) 従属変数の尺度は適切か(名義、順序、間隔、比率) 独立変数の数は適切か モデルについて説明できるか(独立変数の数 …

no image

dataanalysis-002-week2

Structure of Data Analysis 質問を定義する 理想的なデータセットの定義 アクセスできるデータの検索 データの取得 クレンジング Exploratory 統計的予測・モデリング …

2019年9月
月 火 水 木 金 土 日
« 8月   10月 »
 1
2345678
9101112131415
16171819202122
23242526272829
30  

side bar top



アーカイブ

カテゴリー