如何将主成分分析的结果映射回输入模型的实际特征?

当我运行下面的代码时,我看到了’pca.explained_variance_ratio_‘和一个直方图,显示了每个特征解释的方差比例。

import statsmodels.api as smimport numpy as npimport pandas as pdimport statsmodels.formula.api as smffrom statsmodels.stats import anovamtcars = sm.datasets.get_rdataset("mtcars", "datasets", cache=True).datadf = pd.DataFrame(mtcars)x = df.iloc[:,2:]from sklearn.preprocessing import StandardScalerpca = PCA(n_components=11)principalComponents = pca.fit_transform(df)# Plotting the variances for each PCPC = range(1, pca.n_components_+1)plt.bar(PC, pca.explained_variance_ratio_, color='gold')plt.xlabel('Principal Components')plt.ylabel('Variance %')plt.xticks(PC)

enter image description here

如何将PCA 1和2映射回数据框中的原始特征?


回答:

每个主成分都是你变量的线性组合。例如,PC1将是(其中X1,X2代表每个因变量):

enter image description here

所以w11,w22将是你的载荷,它们将代表每个特征对相关主成分的影响。基本上,它们会显示与你的主成分的相关性。参见这个帖子或者像这样的博客,了解使用sklearn的解释。

在你的例子中,你在进行主成分分析之前没有对数据进行标准化处理,所以加载量最大的向量将是幅度最大的那个。所以我们来正确地做这件事:

enter image description here

这些是载荷:

PCnames = ['PC'+str(i+1) for i in range(pca.n_components_)]Loadings = pd.DataFrame(pca.components_,columns=PCnames,index=df.columns)Loadings.iloc[:,:2]                PC1        PC2    mpg      0.362531   -0.373916    cyl      0.016124    0.043744    disp    -0.225744   -0.175311    hp      -0.022540   -0.002592    drat     0.102845    0.058484    wt      -0.108797    0.168554    qsec     0.367724    0.057278    vs       0.754091    0.230825    am      -0.235702   -0.054035    gear     0.139285   -0.846419    carb     0.124896    0.140695 

大致来说,这意味着具有高vs值的观测值将在第一主成分(PC1)上得分较高,而具有高am值的观测值将得分较低。你可以检查它们的影響:

Loadings["PC1"].sort_values().plot.barh()

enter image description here

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注