简体   繁体   English

Python | SKlearn | PCA

[英]Python | SKlearn | PCA

Edit: Thanks for spotting the typo, it should be 60*50, i have corrected the same in the question. 编辑:感谢您发现拼写错误,它应该是60 * 50,我已经纠正了同样的问题。

I am stuck on the following problem, After performing PCA on a matrix with 60 observations and 50 variables when i checked the shape of pca component it comes out to be 50*50. 我陷入了以下问题,当我检查pca组件的形状时,在具有60个观察值和50个变量的矩阵上执行PCA后,结果为50 * 50。 Whereas i think it should be 60*50. 而我认为应该是60 * 50。 Same i checked in R, it comes out to be, as per my understanding, 60*50. 根据我的理解,我在R中签入的内容也是60 * 50。 Please let me know if i am doing something wrong. 如果我做错了事,请告诉我。 PFB the code: PFB代码:

import numpy as np
arr=np.random.randn(20*3*50)
from numpy import *
arr = (arr - mean(arr, axis=0)) / std(arr, axis=0)
arr=arr.reshape(60,50)
arr.shape
#output: (60, 50)

arr[1:20, 2] = 1
arr[21:40, 1] = 2
arr[21:40, 2] = 2
arr[41:60, 1] = 1
arr.shape
#output: (60, 50)

from sklearn.decomposition import PCA
pca = PCA()
X_train_pca = pca.fit_transform(arr)
pca.components_.shape
#output: (50, 50)

Look at PCA class in scikit-learn. 查看 scikit-learn中的PCA类。 It tells us that: 它告诉我们:

...if n_components is not set all components are kept: ...如果未设置n_components,则保留所有组件:

n_components == min(n_samples, n_features)

As far as pca.components_ returns array of shape (n_components, n_features) , there is no confusion. 只要pca.components_返回形状数组(n_components, n_features) ,就不会造成混淆。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM