如何使用 sklearn.decomposition 包中 PCA 的 'components_' 属性将新点投影到新基础?

How to project a new point to new basis using 'components_' attribute of PCA from sklearn.decomposition package?

我有一些具有 3 个坐标的数据点,使用 PCA 函数我通过这样做将其转换为具有 2 个坐标的点

import numpy as np
from sklearn.decomposition import PCA
X = np.array([[-1, -1, -3], [-2, -1, -1], [-3, -2, -2], [1, 1, 1], [2, 1, 5], [3, 2, 6]]) #data
pca = PCA(n_components=2)
pca.fit(X)
PCA(copy=True, n_components=2, whiten=False)
XT = pca.fit_transform(X)
print XT
#output obtained
#[[-4.04510516 -1.24556106]
#[-2.92607624  0.61239898]
#[-4.55000611  1.13825234]
#[ 0.81687144 -1.11632484]
#[ 4.5401931   0.56854397]
#[ 6.16412297  0.04269061]]

我在特征 space 中得到了主轴,使用 'components_' 属性

表示数据中最大方差的方向
W = (pca.components_)
print W
# output obtained
#[[ 0.49508794  0.3217835   0.80705843]
# [-0.67701709 -0.43930775  0.59047148]]

现在我想使用 'components_' 属性将第一个点 [-1, -1, -3](这是 X 中的第一个点)投影到 2D subspace 上 'components_' =15=]

projectedXT_0 = np.dot(W,X[0])
print projectedXT_0
#output obtained
#[-3.23804673 -0.65508959]

#expected output
#[-4.04510516 -1.24556106] 

我没有得到预期的结果,显然我在使用 'components_' 属性计算 projectedPoint 时做错了什么。请演示如何使用 'components_' 属性来获取点的投影。

注意:我知道 'transform' 函数可以执行此操作,但我想使用 'components_' 属性执行此操作。

你忘了减去均值。

查看 pca transform 的来源:

if self.mean_ is not None:
    X = X - self.mean_
X_transformed = fast_dot(X, self.components_.T)
if self.whiten:
    X_transformed /= np.sqrt(self.explained_variance_)
return X_transformed