【问题标题】:What does the sklearn PCA to the input array when when the number of components is choose to be the same?当组件的数量选择相同时,sklearn PCA对输入数组的作用是什么?
【发布时间】:2021-03-19 23:51:37
【问题描述】:

例如我们有:

from sklearn.decomposition import PCA
import numpy as np 

xx = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
pca = PCA()
pca.fit_transform(xx)

输出:

array([[ 1.38340578,  0.2935787 ],
   [ 2.22189802, -0.25133484],
   [ 3.6053038 ,  0.04224385],
   [-1.38340578, -0.2935787 ],
   [-2.22189802,  0.25133484],
   [-3.6053038 , -0.04224385]])

在这种情况下,我并没有减小大小,而是更改了数组...为什么?

【问题讨论】:

标签: machine-learning scikit-learn pca


【解决方案1】:

PCA 对特征空间进行线性(旋转)变换。在你的情况下, 假设特征 1 沿着 x 并且特征 2 沿着 y,则生成的转换与将特征向量旋转角度 theta ~ 2.565 弧度相同。下面我定义了这样一个旋转矩阵,并显示你得到相同的结果:

import numpy as np
def rot_matrix(theta):
    # returns rotation matrix through angle theta
    rotation_matrix = np.dot(np.array([[np.cos(theta), -

np.sin(theta)], [np.sin(theta), np.cos(theta)]])
        return rotation_matrix

theta = 2.565
rot = rot_matrix(theta)
np.dot(rot, xx.T).T

结果是(接近)PCA 变换的输出:

array([[ 1.38349574,  0.29315446],
       [ 2.22182084, -0.25201619],
       [ 3.60531658,  0.04113827],
       [-1.38349574, -0.29315446],
       [-2.22182084,  0.25201619],
       [-3.60531658, -0.04113827]])

【讨论】:

  • 非常感谢您的回答!如果我决定在训练数据集上执行此轮换,我想知道这将如何影响错误?我的意思是我已经尝试过并且我得到了与均方根误差相同的结果......这有意义吗?这类似于尝试减少数据中的噪音吗?
猜你喜欢
  • 2020-12-22
  • 2019-05-17
  • 2018-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-03
相关资源
最近更新 更多