python - 我应该如何使用其 `.components` 编写代码 scikit-learn PCA `.transform()` 方法?
问题描述
我应该如何使用它来编写代码 scikit-learn PCA.transform()
方法.components
?
我认为 PCA.transform()
方法只需将矩阵M
应用于 3D 点,即可将 3D 点转换为 2D 点,P
如下所示:
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
,它表示“它们足够接近”。
if np.allclose(my_transformed2d, pca_transformed2d[sample_index]):
print("My transformation is correct!")
else:
print("My transformation is not correct...")
推荐阅读
- javascript - TypeError: element.getRootNode 在测试时不是函数
- linux - 如何从具有相对路径的另一个文件夹中调用/使用脚本或 conf 文件?
- kotlin - 在注释参数/参数中使用@ConfigurationProperties?(必须是编译时常量)
- kotlin - 当我在 Fragment 中使用 onViewCreated 时,出现了后期初始化错误
- java - 无法将某些日期解析为 ZonedDateTime
- next.js - 尝试部署 Next.js 应用程序时出现“仅支持绝对 URL”错误
- linux - 带有输入的 Bash 函数失败 awk 命令
- ios - 通知附件有时不会快速显示
- python - 使用 Streamlit + Altair 绘制多系列数据框
- javascript - 如何在 vb.net 中的 ICallbackEventHandler.RaiseCallbackEvent 中调用 Javascript 函数