Search code examples
pythonscikit-learnlinear-discriminant

How to explain variables weight from a Linear Discriminant Analysis?


I'm working on a supervised classification. To begin I would like to find variables which have important weight to discriminate each class. My code is the following :

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_)) 

The result is :

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

Then I try this :

lda.covariance_

And I obtain an error :

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

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

Do you have any idea to solve that problem? Morever, if you know to create a correlation circle it would be great!

Thank you.


Solution

  • You have to specify that you want the covariance to be stored when you create the LDA...

    To solve this:

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

    That should do it

    Cheers

    EDIT: for the correlation circle see Plot a Correlation Circle in Python