machine-learning - Scikit-learn PCA .fit_transform 形状不一致(n_samples << m_attributes)

标签 machine-learning scikit-learn pca dimensionality-reduction principal-components

我使用 sklearn 为我的 PCA 获取了不同的形状。 为什么我的转换没有像文档所说的那样产生相同尺寸的数组?

fit_transform(X, y=None)
Fit the model with X and apply the dimensionality reduction on X.
Parameters: 
X : array-like, shape (n_samples, n_features)
Training data, where n_samples is the number of samples and n_features is the number of features.
Returns:    
X_new : array-like, shape (n_samples, n_components)

用 iris 数据集来检查一下,该数据集是 (150, 4),我正在其中制作 4 台 PC:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.preprocessing import StandardScaler
from sklearn import decomposition
import seaborn as sns; sns.set_style("whitegrid", {'axes.grid' : False})

%matplotlib inline
np.random.seed(0)

# Iris dataset
DF_data = pd.DataFrame(load_iris().data, 
                       index = ["iris_%d" % i for i in range(load_iris().data.shape[0])],
                       columns = load_iris().feature_names)

Se_targets = pd.Series(load_iris().target, 
                       index = ["iris_%d" % i for i in range(load_iris().data.shape[0])], 
                       name = "Species")

# Scaling mean = 0, var = 1
DF_standard = pd.DataFrame(StandardScaler().fit_transform(DF_data), 
                           index = DF_data.index,
                           columns = DF_data.columns)

# Sklearn for Principal Componenet Analysis

# Dims
m = DF_standard.shape[1]
K = m

# PCA (How I tend to set it up)
M_PCA = decomposition.PCA()
A_components = M_PCA.fit_transform(DF_standard)
#DF_standard.shape, A_components.shape
#((150, 4), (150, 4))

但是当我在实际数据集 (76, 1989) 上使用与 76 个样本1989 属性/维度 相同的方法时> 我得到一个 (76, 76) 数组,而不是 (76, 1989)

DF_centered = normalize(DF_mydata, method="center", axis=0)
m = DF_centered.shape[1]
# print(m)
# 1989
M_PCA = decomposition.PCA(n_components=m)
A_components = M_PCA.fit_transform(DF_centered)
DF_centered.shape, A_components.shape
# ((76, 1989), (76, 76))

normalize 只是我制作的一个包装器,它从每个维度中减去mean

最佳答案

(注意:此答案改编 self 在 Cross Validated 上的回答:Why are there only n−1 principal components for n data points if the number of dimensions is larger or equal than n? )

PCA(最常见的运行方式)通过以下方式创建新的坐标系:

  1. 将原点移动到数据的质心,
  2. 挤压和/或拉伸(stretch)轴以使它们长度相等,并且
  3. 将轴旋转到新方向。

(有关更多详细信息,请参阅这个优秀的 CV 线程:Making sense of principal component analysis, eigenvectors & eigenvalues。)但是,步骤 3 以非常具体的方式旋转轴。您的新 X1(现在称为“PC1”,即第一个主成分)面向数据的最大变化方向。第二主成分定向在与第一主成分正交的下一个最大变化量的方向上。其余主成分同样形成。

考虑到这一点,让我们看一个简单的示例(由 @amoeba 在 comment 中建议)。这是一个在三维空间中包含两个点的数据矩阵:

X = [ 1 1 1 
      2 2 2 ]

让我们在(伪)三维散点图中查看这些点:

enter image description here

所以让我们按照上面列出的步骤进行操作。 (1) 新坐标系的原点位于(1.5,1.5,1.5)。 (2) 轴已经相等。 (3) 第一个主成分将从原来的(0,0,0)沿对角线移动到原来的(3,3,3),这是这些数据变化最大的方向。现在,第二个主成分必须与第一个主成分正交,并且应该朝着最大剩余变化的方向。但那是什么方向呢?是从(0,0,3)到(3,3,0),还是从(0,3,0)到(3,0,3),还是其他什么?没有剩余的变异,因此不能有更多的主成分。

对于 N=2 的数据,我们最多可以拟合 N−1=1 个主成分。

关于machine-learning - Scikit-learn PCA .fit_transform 形状不一致(n_samples << m_attributes),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38276047/

相关文章:

python - 完美分类时计算ID3算法中属性的熵

python - 是否可以在 keras 中训练多种图像尺寸?

python - 如何在不分割数据的情况下运行sklearn.model_selection.GridSearchCV?

python - python中的sklearn MultinomialNB输入形状错误

matlab - 在 Matlab 中使用不同函数计算的主成分

python - Keras 模型通过编译但在运行时因值错误而崩溃

python-2.7 - Python - 使用朴素贝叶斯选择来自模型

machine-learning - PCA、TruncatedSVD 和 ICA 之间的详细区别是什么?

opencv - OpenCV 的增量奇异值分解

machine-learning - Julia ML : Is there a recommended data format for loading data to Flux, Knet,深度学习库