我正在学习python,并尝试将我的R代码复制到python。复制时,我发现R prcomp和python sklearn PCA的结果不同。
这是我的带有虹膜数据集的python&R代码。
import pandas as pd
import numpy as np
from sklearn.decomposition import PCA
iris = datasets.load_iris()
iris = iris['data']
pca_iris = PCA(n_components=4)
pca_iris_ = pca_iris.fit_transform(iris)
pca_iris_df = pd.DataFrame(data = pca_iris_)pca_iris_df.head(10)
0 1 2 3
0 -2.684126 0.319397 -0.027915 -0.002262
1 -2.714142 -0.177001 -0.210464 -0.099027
2 -2.888991 -0.144949 0.017900 -0.019968
3 -2.745343 -0.318299 0.031559 0.075576
4 -2.728717 0.326755 0.090079 0.061259
5 -2.280860 0.741330 0.168678 0.024201
6 -2.820538 -0.089461 0.257892 0.048143
7 -2.626145 0.163385 -0.021879 0.045298
8 -2.886383 -0.578312 0.020760 0.026745
9 -2.672756 -0.113774 -0.197633 0.056295
head(prcomp(iris[,-5])$x)
PC1 PC2 PC3 PC4
[1,] -2.684126 -0.3193972 0.02791483 0.002262437
[2,] -2.714142 0.1770012 0.21046427 0.099026550
[3,] -2.888991 0.1449494 -0.01790026 0.019968390
[4,] -2.745343 0.3182990 -0.03155937 -0.075575817
[5,] -2.728717 -0.3267545 -0.09007924 -0.061258593
[6,] -2.280860 -0.7413304 -0.16867766 -0.024200858
如您所见,绝对值相同,但PC2,PC3和PC4值不同。有人知道为什么吗?