matplotlib Sklearn特征向量的简单图,分解,PCA

z0qdvdin  于 2022-11-15  发布在  其他
关注(0)|答案(1)|浏览(130)

我正在尝试理解Principal Component Analysis的工作原理,并在sklearn.datasets.load_iris数据集上测试它。我了解每个步骤的工作原理(例如,标准化数据,协方差,特征值分解,排序最高特征值,使用K选定维度将原始数据转换到新轴)。
下一步是可视化这些eigenvectors投影到数据集上的位置(在PC1 vs. PC2 plot上,对吗?)。

有人能解释一下如何在降维数据集的3D图上绘制[PC1,PC2,PC3]特征向量吗?

另外,我画的二维图正确吗?我不知道为什么我的第一个特征向量的长度较短。我应该乘以特征值吗?

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

我使用的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()

deyfvvtc

deyfvvtc1#

我认为你问错了问题。特征向量是主成分(PC1,PC2等)。所以在[PC1,PC2,PC3] 3D图中绘制特征向量只是简单地绘制该图的三个正交轴。
你可能想把特征向量在原始坐标系中的样子形象化,这就是第二个链接中讨论的内容:Basic example for PCA with matplotlib

相关问题