主成分選取

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...