我使用这个简单的代码在一个有10个特征的数据帧上运行PCA:
pca = PCA()
fit = pca.fit(dfPca)pca.explained_variance_ratio_的结果显示:
array([ 5.01173322e-01, 2.98421951e-01, 1.00968655e-01,
4.28813755e-02, 2.46887288e-02, 1.40976609e-02,
1.24905823e-02, 3.43255532e-03, 1.84516942e-03,
4.50314168e-16])我认为这意味着第一个PC解释了52%的方差,第二个成分解释了29%,以此类推……
我不理解的是pca.components_的输出。如果我执行以下操作:
df = pd.DataFrame(pca.components_, columns=list(dfPca.columns))我得到了下面的数据帧,其中每行都是一个主成分。我想要理解的是如何解释这个表。我知道如果我将每个组件上的所有特征平方并求和,我得到的是1,但是PC1上的-0.56是什么意思?它是否告诉了一些关于“特征E”的东西,因为它是解释52%方差的分量的最高量级?

谢谢
发布于 2017-11-19 06:35:54
术语:首先,主成分分析的结果通常是根据成分分数来讨论的,有时称为因子分数(对应于特定数据点的转换后的变量值)和负载(每个标准化原始变量应乘以得到成分分数的权重)。
PART1:我解释了如何检查特征的重要性以及如何绘制双线图。
PART2:我解释了如何检查特性的重要性,以及如何使用特性名称将它们保存到pandas数据帧中。
一篇文章摘要: Python compact guide:https://towardsdatascience.com/pca-clearly-explained-how-when-why-to-use-it-and-feature-importance-a-guide-in-python-7c274582c37e?source=friends_link&sk=65bf5440e444c24aff192fedf9f8b64f
第1部分:
在您的示例中,功能E的值为-0.56,即此功能在PC1上的得分。此值告诉我们该功能对PC (在本例中为PC1)的影响程度。
因此,绝对值越高,对主成分的影响越大。
在执行PCA分析之后,人们通常会绘制已知的“双向图”,以查看N维(在我们的例子中为2)的变换特征和原始变量(特征)。
我写了一个函数来绘制这个。
使用虹膜数据的示例:
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
iris = datasets.load_iris()
X = iris.data
y = iris.target
#In general it is a good idea to scale the data
scaler = StandardScaler()
scaler.fit(X)
X=scaler.transform(X)
pca = PCA()
pca.fit(X,y)
x_new = pca.transform(X)
def myplot(score,coeff,labels=None):
xs = score[:,0]
ys = score[:,1]
n = coeff.shape[0]
plt.scatter(xs ,ys, c = y) #without scaling
for i in range(n):
plt.arrow(0, 0, coeff[i,0], coeff[i,1],color = 'r',alpha = 0.5)
if labels is None:
plt.text(coeff[i,0]* 1.15, coeff[i,1] * 1.15, "Var"+str(i+1), color = 'g', ha = 'center', va = 'center')
else:
plt.text(coeff[i,0]* 1.15, coeff[i,1] * 1.15, labels[i], color = 'g', ha = 'center', va = 'center')
plt.xlabel("PC{}".format(1))
plt.ylabel("PC{}".format(2))
plt.grid()
#Call the function.
myplot(x_new[:,0:2], pca. components_)
plt.show()结果

第2部分:
重要的特征是那些对组件影响更大的特征,因此在组件上具有较大的绝对值。
要使用名称获取PC上最重要的功能,并将其保存到pandas dataframe中,请使用以下命令:
from sklearn.decomposition import PCA
import pandas as pd
import numpy as np
np.random.seed(0)
# 10 samples with 5 features
train_features = np.random.rand(10,5)
model = PCA(n_components=2).fit(train_features)
X_pc = model.transform(train_features)
# number of components
n_pcs= model.components_.shape[0]
# get the index of the most important feature on EACH component
# LIST COMPREHENSION HERE
most_important = [np.abs(model.components_[i]).argmax() for i in range(n_pcs)]
initial_feature_names = ['a','b','c','d','e']
# get the names
most_important_names = [initial_feature_names[most_important[i]] for i in range(n_pcs)]
# LIST COMPREHENSION HERE AGAIN
dic = {'PC{}'.format(i): most_important_names[i] for i in range(n_pcs)}
# build the dataframe
df = pd.DataFrame(dic.items())打印此内容:
0 1
0 PC0 e
1 PC1 d因此,在PC1上,名为d**.** PC1的功能是最重要的;在上,这项功能是最重要的
文章摘要: Python compact guide:https://towardsdatascience.com/pca-clearly-explained-how-when-why-to-use-it-and-feature-importance-a-guide-in-python-7c274582c37e?source=friends_link&sk=65bf5440e444c24aff192fedf9f8b64f
发布于 2017-11-19 05:15:19
Basic Idea
根据特征的主成分分解基本上告诉你每个主成分在特征方向方面所指向的“方向”。
在每个主成分中,具有较大绝对权重的特征将主成分更多地拉向该特征的方向。
例如,我们可以说,在PC1中,由于特征A、特征B、特征I和特征J具有相对较低的权重(绝对值),因此PC1在特征空间中没有那么多地指向这些特征的方向。相对于其他方向,PC1将主要指向特征E的方向。
低维中的可视化
为了直观地了解这一点,请看以下取自here和here的图
下面显示了对相关数据运行PCA的示例。

我们可以直观地看到,从PCA派生的两个特征向量在特征1和特征2方向上都被“拉”了。因此,如果我们要制作一个像您制作的主成分分解表,我们预计会看到来自特征1和特征2的一些权重来解释PC1和PC2。
接下来,我们有一个不相关数据的示例。

让我们将绿色的主成分称为PC1,将粉色的主成分称为PC2。很明显,PC1不是沿着特征x‘的方向拉的,PC2也不是沿着特征y’的方向拉的。因此,在我们的表中,PC1中的特征x‘的权重必须为0,而PC2中的特征y’的权重必须为0。
我希望这能让您对表中的内容有所了解。
https://stackoverflow.com/questions/47370795
复制相似问题