我应该如何使用其 `.components` 编写代码 scikit-learn PCA `.transform()` 方法?
How should I write the code scikit-learn PCA `.transform()` method by using its `.components`?
我应该如何使用其 .components
编写代码 scikit-learn PCA .transform()
方法?
我认为 PCA .transform()
方法只需将矩阵 M
应用于 3D 点 P
即可将 3D 点转换为 2D 点,如下所示:
np.dot(M, P)
为确保这是正确的,我编写了以下代码。
但是,结果是,我无法用 PCA .transform()
方法得出相同的结果。
我应该如何修改代码?我错过了什么吗?
from sklearn.decomposition import PCA
import numpy as np
data3d = np.arange(10*3).reshape(10, 3) ** 2
pca = PCA(n_components=2)
pca.fit(data3d)
pca_transformed2d = pca.transform(data3d)
sample_index = 0
sample3d = data3d[sample_index]
# Manually transform `sample3d` to 2 dimensions.
w11, w12, w13 = pca.components_[0]
w21, w22, w23 = pca.components_[1]
my_transformed2d = np.zeros(2)
my_transformed2d[0] = w11 * sample3d[0] + w12 * sample3d[1] + w13 * sample3d[2]
my_transformed2d[1] = w21 * sample3d[0] + w22 * sample3d[1] + w23 * sample3d[2]
print("================ Validation ================")
print("pca_transformed2d:", pca_transformed2d[sample_index])
print("my_transformed2d:", my_transformed2d)
if np.all(my_transformed2d == pca_transformed2d[sample_index]):
print("My transformation is correct!")
else:
print("My transformation is not correct...")
输出:
================ Validation ================
pca_transformed2d: [-492.36557212 12.28386702]
my_transformed2d: [ 3.03163093 -2.67255444]
My transformation is not correct...
PCA 以居中 数据开始:减去所有观测值的平均值。在这种情况下,居中是通过
完成的
centered_data = data3d - data3d.mean(axis=0)
沿 axis=0(行)取平均值意味着只剩下一行,其中包含平均值的三个分量。居中后,将数据乘以 PCA 分量;但我不会手写矩阵乘法,而是使用 .dot
:
my_transformed2d = pca.components_.dot(centered_data[sample_index])
最后,验证。不要在浮点数之间使用 ==
;完全平等是罕见的。由于某处的不同操作顺序而出现微小差异:例如,
0.1 + 0.2 - 0.3 == 0.1 - 0.3 + 0.2
为假。这就是为什么我们有 np.allclose
,也就是 "they are close enough"。
if np.allclose(my_transformed2d, pca_transformed2d[sample_index]):
print("My transformation is correct!")
else:
print("My transformation is not correct...")
我应该如何使用其 .components
编写代码 scikit-learn PCA .transform()
方法?
我认为 PCA .transform()
方法只需将矩阵 M
应用于 3D 点 P
即可将 3D 点转换为 2D 点,如下所示:
np.dot(M, P)
为确保这是正确的,我编写了以下代码。
但是,结果是,我无法用 PCA .transform()
方法得出相同的结果。
我应该如何修改代码?我错过了什么吗?
from sklearn.decomposition import PCA
import numpy as np
data3d = np.arange(10*3).reshape(10, 3) ** 2
pca = PCA(n_components=2)
pca.fit(data3d)
pca_transformed2d = pca.transform(data3d)
sample_index = 0
sample3d = data3d[sample_index]
# Manually transform `sample3d` to 2 dimensions.
w11, w12, w13 = pca.components_[0]
w21, w22, w23 = pca.components_[1]
my_transformed2d = np.zeros(2)
my_transformed2d[0] = w11 * sample3d[0] + w12 * sample3d[1] + w13 * sample3d[2]
my_transformed2d[1] = w21 * sample3d[0] + w22 * sample3d[1] + w23 * sample3d[2]
print("================ Validation ================")
print("pca_transformed2d:", pca_transformed2d[sample_index])
print("my_transformed2d:", my_transformed2d)
if np.all(my_transformed2d == pca_transformed2d[sample_index]):
print("My transformation is correct!")
else:
print("My transformation is not correct...")
输出:
================ Validation ================
pca_transformed2d: [-492.36557212 12.28386702]
my_transformed2d: [ 3.03163093 -2.67255444]
My transformation is not correct...
PCA 以居中 数据开始:减去所有观测值的平均值。在这种情况下,居中是通过
完成的centered_data = data3d - data3d.mean(axis=0)
沿 axis=0(行)取平均值意味着只剩下一行,其中包含平均值的三个分量。居中后,将数据乘以 PCA 分量;但我不会手写矩阵乘法,而是使用 .dot
:
my_transformed2d = pca.components_.dot(centered_data[sample_index])
最后,验证。不要在浮点数之间使用 ==
;完全平等是罕见的。由于某处的不同操作顺序而出现微小差异:例如,
0.1 + 0.2 - 0.3 == 0.1 - 0.3 + 0.2
为假。这就是为什么我们有 np.allclose
,也就是 "they are close enough"。
if np.allclose(my_transformed2d, pca_transformed2d[sample_index]):
print("My transformation is correct!")
else:
print("My transformation is not correct...")