Python SKlearn PCA

2024-05-12 15:40:59 发布

您现在位置:Python中文网/ 问答频道 /正文

编辑:谢谢你发现的错别字,它应该是60*50,我已经纠正了同样的问题。你知道吗

在对一个包含60个观测值和50个变量的矩阵进行主成分分析后,我陷入了下面的问题,当我检查主成分分析的形状时,结果是50*50。而我认为应该是60*50。和我在R登记的一样,据我所知,是60*50。如果我做错了什么,请告诉我。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)

Tags: fromimportnumpy编辑outputnp矩阵形状
1条回答
网友
1楼 · 发布于 2024-05-12 15:40:59

Look在scikit学习的PCA课上。它告诉我们:

...if n_components is not set all components are kept:

n_components == min(n_samples, n_features)

只要pca.components_返回形状(n_components, n_features)的数组,就没有混淆。你知道吗

相关问题 更多 >