When using PCA in sklearn, it's easy to get out the components:
from sklearn import decomposition
pca = decomposition.PCA(n_components=n_components)
pca_data = pca.fit(input_data)
pca_components = pca.components_
But I can't for the life of me figure out how to get the components out of LDA, as there is no components_ attribute. Is there a similar attribute in sklearn lda?
In the case of PCA, the documentation is clear. The
pca.components_
are the eigenvectors.In the case of LDA, we need the
lda.scalings_
attribute.Visual example using iris data and sklearn:
Verify that the lda.scalings_ are the eigenvectors:
Additionally here is a useful function to plot the biplot and verify visually:
Results
My reading of the code is that the
coef_
attribute is used to weight each of the components when scoring a sample's features against the different classes.scaling
is the eigenvector andxbar_
is the mean. In the spirit of UTSL, here's the source for the decision function: https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/lda.py#L188In PCA, the transform operation uses
self.components_.T
(see the code):In LDA, the transform operation uses
self.scalings_
(see the code):Note the
.T
which transposes the array in the PCA, and not in LDA:components_ : array, shape (n_components, n_features)
scalings_ : array, shape (n_features, n_classes - 1)