主成分の選択

Pythonで学ぶ次元削減

Jeroen Boeye

Head of Machine Learning, Faktion

寄与率のしきい値を設定

pipe = Pipeline([
        ('scaler', StandardScaler()),
        ('reducer', PCA(n_components=0.9))])

# Fit the pipe to the data pipe.fit(poke_df) print(len(pipe['reducer'].components_))
5
Pythonで学ぶ次元削減

最適な成分数

pipe.fit(poke_df)

var = pipe['reducer'].explained_variance_ratio_

plt.plot(var)

plt.xlabel('Principal component index')
plt.ylabel('Explained variance ratio')
plt.show()

寄与率のプロット

Pythonで学ぶ次元削減

最適な成分数

pipe.fit(poke_df)

var = pipe['reducer'].explained_variance_ratio_

plt.plot(var)

plt.xlabel('Principal component index')
plt.ylabel('Explained variance ratio')
plt.show()

寄与率エルボー図

Pythonで学ぶ次元削減

PCA の操作

PCA フィット・変換の概念図 1

Pythonで学ぶ次元削減

PCA の操作

PCA フィット・変換の概念図 2

Pythonで学ぶ次元削減

PCA の操作

PCA フィット・変換の概念図 3

Pythonで学ぶ次元削減

画像の圧縮

元の顔画像

Pythonで学ぶ次元削減

画像の圧縮

print(X_test.shape)
(15, 2914)

62 x 47 ピクセル = 2914 濃淡値

print(X_train.shape)
(1333, 2914)
Pythonで学ぶ次元削減

画像の圧縮

pipe = Pipeline([
        ('scaler', StandardScaler()),
        ('reducer', PCA(n_components=290))])

pipe.fit(X_train)
pc = pipe.fit_transform(X_test) print(pc.shape)
(15, 290)
Pythonで学ぶ次元削減

画像の再構成

pc = pipe.transform(X_test)

print(pc.shape)
(15, 290)
X_rebuilt = pipe.inverse_transform(pc)

print(X_rebuilt.shape)
(15, 2914)
img_plotter(X_rebuilt)

再構成した顔画像

Pythonで学ぶ次元削減

画像の再構成

元の顔画像

再構成した顔画像

Pythonで学ぶ次元削減

練習しましょう!

Pythonで学ぶ次元削減

Preparing Video For Download...