sklearn.decomposition.PCA

时间:2016-06-22 19:20:16

标签: python matplotlib machine-learning scikit-learn pca

我试图了解Principal Component Analysis的工作原理,我正在sklearn.datasets.load_iris数据集上对其进行测试。我理解每个步骤是如何工作的(例如,标准化数据,协方差,特征分解,对最高特征值进行排序,使用K选定维度将原始数据转换为新轴。

下一步是可视化这些eigenvectors投射到数据集上的位置(在PC1 vs. PC2 plot上,对吧?)。

有人可以解释如何在缩小尺寸数据集的3D图上绘制[PC1,PC2,PC3]特征向量吗?

另外,我是否正确地绘制了这个2D版本?我不确定为什么我的第一个特征向量的长度较短。我应该乘以特征值吗?

以下是我为实现这一目标所做的一些研究:

我关注的PCA方法来自: https://plot.ly/ipython-notebooks/principal-component-analysis/#Shortcut---PCA-in-scikit-learn(虽然我不想使用plotly。我想坚持pandas, numpy, sklearn, matplotlib, scipy, and seaborn

我一直在关注绘制特征向量的本教程,看起来很简单:Basic example for PCA with matplotlib但我似乎无法用我的数据复制结果。

我发现了这一点,但对于我尝试做的事情似乎过于复杂,我不想创建FancyArrowPatchplotting the eigenvector of covariance matrix using matplotlib and np.linalg

我试图让我的代码尽可能简单地遵循其他教程:

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 = 2

# PCA (How I tend to set it up)
M_PCA = decomposition.PCA(n_components=m)
DF_PCA = pd.DataFrame(M_PCA.fit_transform(DF_standard), 
                columns=["PC%d" % k for k in range(1,m + 1)]).iloc[:,:K]


# Plot the eigenvectors
#https://stackoverflow.com/questions/18299523/basic-example-for-pca-with-matplotlib

# This is where stuff gets weird...
data = DF_standard

mu = data.mean(axis=0)
eigenvectors, eigenvalues = M_PCA.components_, M_PCA.explained_variance_ #eigenvectors, eigenvalues, V = np.linalg.svd(data.T, full_matrices=False)
projected_data = DF_PCA #np.dot(data, eigenvectors)

sigma = projected_data.std(axis=0).mean()

fig, ax = plt.subplots(figsize=(10,10))
ax.scatter(projected_data["PC1"], projected_data["PC2"])
for axis, color in zip(eigenvectors[:K], ["red","green"]):
#     start, end = mu, mu + sigma * axis ### leads to "ValueError: too many values to unpack (expected 2)"

    # So I tried this but I don't think it's correct
    start, end = (mu)[:K], (mu + sigma * axis)[:K] 
    ax.annotate('', xy=end,xytext=start, arrowprops=dict(facecolor=color, width=1.0))

ax.set_aspect('equal')
plt.show()

enter image description here

1 个答案:

答案 0 :(得分:0)

我认为你问的是错误的问题。特征向量是主要成分(PC1,PC2等)。因此,绘制[PC1,PC2,PC3] 3D图中的特征向量只是绘制该图的三个正交轴。

您可能希望可视化特征向量在原始坐标系中的外观。这是您的第二个链接中讨论的内容:Basic example for PCA with matplotlib