首页 > 解决方案 > 具有 2 个组件的 pca 输出 1 个功能

问题描述

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

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) ?

标签: pythonnumpyscikit-learn

解决方案


是的,这是预期的行为。

根据PCA 的文档

actual n_components = min(n_samples, specified n_components)

在这里n_samples = 1(如您在 中所示) ,PCA 返回Input.shape的实际值为 1。n_components


推荐阅读