科学の箱

科学・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

irisをナイーブベイズで分類

ナイーブベイズの概要 ナイーブベイズは教師ありの分類アルゴリズムの一つである。 計算アルゴリズムとしてはベイズ定理を用いている。 機械学習における特徴としてはいかがある 実装が簡単であり、複雑なハイパ …

no image

仕事で始める機械学習 – 6章 効果検証

効果検証のステップ 問題認識: 顧客の機器利用率が低い 問題の影響: 長期的なメンテナンス契約からの利益の確保 課題設定候補: メンテナンス以外からの利益確保 新規顧客からの利益 顧客利用率を上げる …

no image

SIGNATE お弁当の需要予測-1

SIGNATEのコンペであるお弁当の需要予測をpythonで分析 データは下記から取得できる https://signate.jp/competitions/24 ライブラリ読み込み importnu …

no image

R Dataset – bone

データの説明 261人の子供たちから得られた年齢別骨密度。 フォーマット idnum: 識別コード age: 測定時の年齢 gender: 性別 spnbmd: 骨密度 チェック テーブル全体について …

no image

仕事で始める機械学習 – 1. 機械学習プロジェクトの始め方

機械学習プロジェクトの流れ 問題の定式化 機械学習を利用しない方法 システム設計 アルゴリズム選定 特徴量・教師データ・ログの設計 前処理 学習・パラメータチューニング システム統合 問題の定式化 目 …

2019年9月
« 8月   10月 »
 1
2345678
9101112131415
16171819202122
23242526272829
30  

side bar top



アーカイブ

カテゴリー