Skip to main content
 首页 » 编程设计

python之具有 2 个组件的 pca 输出 1 个特征

2025年01月19日9kerrycode

我有一个包含 10 个元素的列表,我选择了 5 个特征来创建我的输入并将输入列表转换为一个数组;然后我应用 pca,组件数 = 2:

idx = {0, 2, 3, 7, 8} 
Input = array([Input[x] for x in idx]).reshape((1, -1))  
print (Input.shape) # prints (1, 5) 
 
pca = PCA(n_components=2).fit(Input)  
Input = pca.transform(Input)  
print (Input.shape) #prints (1, 1) 

为什么在 n_components=2 的 pca 之后我的输入形状是 (1,1) 而不是 (1,2)?

请您参考如下方法:

是的,这是预期的行为。

根据 documentation for PCA :

actual n_components = min(n_samples, specified n_components) 

此处有 n_samples = 1(如您在 Input.shape 中所示),因此 PCA 返回的实际 n_components 为 1。