公開日:2019-08-15
最終更新日:2019-08-29
最終更新日:2019-08-29
skl09-0:準備
次のコードを実行しよう.
|
1 2 3 4 5 6 7 8 |
>>> import numpy as np >>> from sklearn import datasets >>> >>> # アヤメデータの読込み >>> iris = datasets.load_iris() >>> >>> iris_X = iris.data >>> iris_y = iris.target |
skl09-1:
PCAのインポートPCAをインポートしよう.
難易度:★
| ミッション | 説明 |
|---|---|
| 1 | PCAをインポートする. |
skl09-2:
PCAオブジェクトの生成PCAオブジェクトを生成しよう.
難易度:★
| ミッション | 説明 |
|---|---|
| 1 | PCA()コンストラクタを呼び出す. |
| 2 | 生成したPCAオブジェクトをpcaとする. |
skl09-3:学習
学習データを基にpcaにより学習しよう.
難易度:★★
| ミッション | 説明 |
|---|---|
| 1 | PCA.fit()メソッドを使う. |
skl09-4:説明変数の分散の取得
PCAの説明変数の分散を取得しよう.
難易度:★★
| ミッション | 説明 |
|---|---|
| 1 | PCA.explained_variance_属性を使う. |
skl09-5:縮約次元数の設定
縮約次元数を2に設定しよう.
難易度:★★
| ミッション | 説明 |
|---|---|
| 1 | PCA.n_components属性を使う. |
skl09-6:次元縮約
学習データを2次元に縮約しよう.
難易度:★★
| ミッション | 説明 |
|---|---|
| 1 | PCA.fit_transform()メソッドを使う. |
| 2 | 縮約したデータをiris_X_reducedとする. |
skl09-7:縮約データの配列の形状の取得
iris_X_reducedの配列の形状を取得しよう.
難易度:★
| ミッション | 説明 |
|---|---|
| 1 | ndarray.shape属性を使う. |
skl09-8:縮約データの内容の表示
iris_X_reducedの内容を表示しよう.
難易度:★
| ミッション | 説明 |
|---|---|
| 1 | print()関数を使う. |
skl09-9:学習モデルの可視化
次のコードはpcaによる学習モデルを可視化するものである.次のコードをskl09_plt01.pyというファイル名で保存し,python3コマンドで実行しよう.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
import numpy as np import pandas as pd import matplotlib.pyplot as plt from matplotlib.colors import ListedColormap from sklearn import datasets from sklearn.cluster import KMeans from sklearn.decomposition import PCA # data iris = datasets.load_iris() iris_X = iris.data iris_y = iris.target # train pca = PCA() pca.fit(iris_X) pca.n_components = 2 iris_X_reduced = pca.fit_transform(iris_X) # train n_neighbors = 3 k_means = KMeans(n_clusters=n_neighbors) k_means.fit(iris_X_reduced) iris_y_pred = k_means.labels_ # plot cmap_light = ListedColormap(['#CCCCFF', '#CCFFCC', '#FFCCCC']) cmap_dark = ListedColormap(['#8888FF', '#88FF88', '#FF8888']) x_min = np.min(iris_X_reduced[:, 0]) x_max = np.max(iris_X_reduced[:, 0]) y_min = np.min(iris_X_reduced[:, 1]) y_max = np.max(iris_X_reduced[:, 1]) xx, yy = np.mgrid[x_min:x_max:200j, y_min:y_max:200j] Z = k_means.predict(np.c_[xx.ravel(), yy.ravel()]) Z = Z.reshape(xx.shape) #plt.scatter(iris_X_reduced[:, 0], iris_X_reduced[:, 1], color='#FF8888', edgecolors='k') plt.pcolormesh(xx, yy, Z, cmap=cmap_light) plt.scatter(iris_X_reduced[:, 0], iris_X_reduced[:, 1], c=iris_y_pred, cmap=cmap_dark, edgecolors='k') plt.title("iris") plt.xlabel('x1') plt.ylabel('x2') plt.xlim(xx.min(), xx.max()) plt.ylim(yy.min(), yy.max()) plt.show() |
難易度:★
| ミッション | 説明 |
|---|---|
| 1 | python3コマンドでskl09_plt01.pyを実行する. |
skl09-10:散布図行列の可視化
次のコードはデータの散布図行列を可視化するものである.次のコードをskl09_plt02.pyというファイル名で保存し,python3コマンドで実行しよう.
|
1 2 3 4 5 6 7 8 9 10 |
import matplotlib.pyplot as plt import seaborn as sns # data iris = sns.load_dataset('iris') # plot sns.pairplot(iris) plt.show() |
難易度:★
| ミッション | 説明 |
|---|---|
| 1 | python3コマンドでskl09_plt02.pyを実行する. |