Skip to main content
 首页 » 编程设计

python之Python 中 PCA 图中的项目变量

2025年05月04日26sxdcgaq8080

在 R 中执行 PCA 分析后,我们可以:

ggbiplot(pca, choices=1:2, groups=factor(row.names(df_t))) 

这将绘制2PC空间中的数据,以及该空间中变量的方向和权重作为向量(具有不同的长度和方向)。

在 Python 中,我可以在 2 PC 空间中绘制数据,我可以获得变量的权重,但我如何知道方向。

换句话说,我如何在 Python 中绘制对 PC(权重和方向)的变量贡献?

请您参考如下方法:

我不知道这种情节有任何预制实现,但可以使用 matplotlib.pyplot.quiver 创建。这是我快速组合的示例。您可以以此为基础来创建适合您的数据的精美图表。


示例数据

这会生成一些示例数据。它从 this answer 中重复使用.

# User input 
n_samples  = 100 
n_features =   5 
 
# Prep 
data  = np.empty((n_samples,n_features)) 
np.random.seed(42) 
 
# Generate 
for i,mu in enumerate(np.random.choice([0,1,2,3], n_samples, replace=True)): 
    data[i,:] = np.random.normal(loc=mu, scale=1.5, size=n_features) 

主成分分析

pca = PCA().fit(data) 

变量因子图

开始吧:

# Get the PCA components (loadings) 
PCs = pca.components_ 
 
# Use quiver to generate the basic plot 
fig = plt.figure(figsize=(5,5)) 
plt.quiver(np.zeros(PCs.shape[1]), np.zeros(PCs.shape[1]), 
           PCs[0,:], PCs[1,:],  
           angles='xy', scale_units='xy', scale=1) 
 
# Add labels based on feature names (here just numbers) 
feature_names = np.arange(PCs.shape[1]) 
for i,j,z in zip(PCs[1,:]+0.02, PCs[0,:]+0.02, feature_names): 
    plt.text(j, i, z, ha='center', va='center') 
 
# Add unit circle 
circle = plt.Circle((0,0), 1, facecolor='none', edgecolor='b') 
plt.gca().add_artist(circle) 
 
# Ensure correct aspect ratio and axis limits 
plt.axis('equal') 
plt.xlim([-1.0,1.0]) 
plt.ylim([-1.0,1.0]) 
 
# Label axes 
plt.xlabel('PC 0') 
plt.ylabel('PC 1') 
 
# Done 
plt.show() 


不确定

我对箭头的缩放有点费劲。请确保它们正确反射(reflect)了您数据的负载。快速检查 feature 4 是否真的与 PC 1 密切相关(如本例所示)看起来很有希望:

data_pca = pca.transform(data) 
plt.scatter(data_pca[:,1], data[:,4]) 
plt.xlabel('PC 2') and plt.ylabel('feature 4') 
plt.show()