相关疑难解决方法(0)

使用sklearn在PCA中恢复explain_variance_ratio_的功能名称

我正在尝试从使用scikit-learn完成的PCA中恢复,这些功能被选为相关的.

IRIS数据集的典型示例.

import pandas as pd
import pylab as pl
from sklearn import datasets
from sklearn.decomposition import PCA

# load dataset
iris = datasets.load_iris()
df = pd.DataFrame(iris.data, columns=iris.feature_names)

# normalize data
df_norm = (df - df.mean()) / df.std()

# PCA
pca = PCA(n_components=2)
pca.fit_transform(df_norm.values)
print pca.explained_variance_ratio_
Run Code Online (Sandbox Code Playgroud)

这回来了

In [42]: pca.explained_variance_ratio_
Out[42]: array([ 0.72770452,  0.23030523])
Run Code Online (Sandbox Code Playgroud)

如何恢复哪两个特征允许数据集中这两个解释的方差? 不同地说,如何在iris.feature_names中获取此功能的索引?

In [47]: print iris.feature_names
['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']
Run Code Online (Sandbox Code Playgroud)

在此先感谢您的帮助.

python machine-learning pca scikit-learn

61
推荐指数
5
解决办法
4万
查看次数

sklearn上的PCA - 如何解释pca.components_

我使用这个简单的代码在具有10个功能的数据框架上运行PCA:

pca = PCA()
fit = pca.fit(dfPca)
Run Code Online (Sandbox Code Playgroud)

结果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])
Run Code Online (Sandbox Code Playgroud)

我认为这意味着第一台PC解释了52%的差异,第二部分解释了29%等等......

我不明白的是输出pca.components_.如果我执行以下操作:

df = pd.DataFrame(pca.components_, columns=list(dfPca.columns))
Run Code Online (Sandbox Code Playgroud)

我得到的数据框低于每一行是主要成分.我想要了解的是如何解释该表.我知道如果我对每个组件的所有功能进行平方并对它们求和,我得到1,但PC1上的-0.56是什么意思?它告诉了一些关于"特征E"的东西,因为它是一个解释了52%方差的组件的最高等级?

在此输入图像描述

谢谢

python math pca scikit-learn

13
推荐指数
2
解决办法
1万
查看次数

PCA分析后的特征/变量重要性

我对原始数据集进行了PCA分析,并且从PCA转换的压缩数据集中,我还选择了我想要保留的PC数量(它们几乎解释了94%的方差).现在,我正在努力识别在简化数据集中重要的原始特征.在降维后,如何找出哪些特征是重要的,哪些特征不在剩余的主要组件中?这是我的代码:

from sklearn.decomposition import PCA
pca = PCA(n_components=8)
pca.fit(scaledDataset)
projection = pca.transform(scaledDataset)
Run Code Online (Sandbox Code Playgroud)

此外,我还尝试对简化数据集执行聚类算法,但令我惊讶的是,得分低于原始数据集.这怎么可能?

python machine-learning pca feature-selection scikit-learn

12
推荐指数
2
解决办法
1万
查看次数

Python 中的 PLS-DA 加载图

如何使用 PLS-DA 图的 Matplotlib 制作加载图,例如 PCA 的加载图?

这个答案解释了如何使用 PCA 完成: Plot PCA loadings and loading in biplot in sklearn (like R's autoplot)

然而,这两种方法之间存在一些显着差异,这使得实现也不同。(此处解释了一些相关差异https://learnche.org/pid/latent-variable-modelling/projection-to-latent-structures/interpreting-pls-scores-and-loadings

为了制作 PLS-DA 图,我使用以下代码:

from sklearn.preprocessing import StandardScaler
from sklearn.cross_decomposition import PLSRegression
import numpy as np
import pandas as pd

targets = [0, 1]

x_vals = StandardScaler().fit_transform(df.values)

y = [g == targets[0] for g in sample_description]
y = np.array(y, dtype=int)

plsr = PLSRegression(n_components=2, scale=False)
plsr.fit(x_vals, y)

colormap = {
    targets[0]: '#ff0000',  # Red
    targets[1]: '#0000ff',  # …
Run Code Online (Sandbox Code Playgroud)

python matplotlib scikit-learn

7
推荐指数
1
解决办法
910
查看次数

将图例添加到散点图(PCA)

我是python的新手,发现了这个出色的PCA双线图建议(绘制PCA加载和在sklearn中的双线图中加载(如R的自动绘图))。现在,我尝试为图例中的不同目标添加图例。但是该命令plt.legend()不起作用。

有一个简单的方法吗?例如,来自上面链接的虹膜数据和双标码。

import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.decomposition import PCA
import pandas as pd
from sklearn.preprocessing import StandardScaler

iris = datasets.load_iris()
X = iris.data
y = iris.target
#In general a good idea is to scale the data
scaler = StandardScaler()
scaler.fit(X)
X=scaler.transform(X)    

pca = PCA()
x_new = pca.fit_transform(X)

def myplot(score,coeff,labels=None):
    xs = score[:,0]
    ys = score[:,1]
    n = coeff.shape[0]
    scalex = 1.0/(xs.max() - xs.min())
    scaley = 1.0/(ys.max() - …
Run Code Online (Sandbox Code Playgroud)

python matplotlib legend pca biplot

2
推荐指数
1
解决办法
957
查看次数