主成分选择

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 中的降维

Passons à la pratique !

Python 中的降维

Preparing Video For Download...