如何通过线性判别分析解释变量权重?

时间:2019-01-11 13:13:52

标签: python scikit-learn linear-discriminant

我正在进行监督分类。首先,我想找到对每个类别都有重要意义的变量。我的代码如下:

from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA

X = X_train_std[0:1000,:]
y = y_train[0:1000]
target_names = classes

lda = LDA(n_components=2)
X_r2 = lda.fit(X, y).transform(X)

print('explained variance ratio (first two components) with LDA: %s'
  % str(lda.explained_variance_ratio_)) 

结果是:

explained variance ratio (first two components) with LDA: [0.64492115 0.24080238]

然后我尝试一下:

lda.covariance_

我得到一个错误:

AttributeError                            Traceback (most recent call last)
<ipython-input-28-35184940aba0> in <module>
----> 1 lda.covariance_

AttributeError: 'LinearDiscriminantAnalysis' object has no attribute 'covariance_'

您有解决该问题的想法吗?此外,如果您知道要创建一个相关圆,那就太好了!

谢谢。

1 个答案:

答案 0 :(得分:1)

创建LDA时必须指定要存储协方差...

要解决此问题:

lda = LDA(n_components=2, store_covariance=True)

应该这样做

欢呼声

编辑:相关圈子请参见Plot a Correlation Circle in Python