You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
LDA.explained_variance_ratio_ is of the wrong size #6032
The thing is, explained_variance_ratio_ is length n_components for the svd solver, but not for the eigen solver. So changing the doc would be problematic.
I think the "less worse" solution is to go for the code. But this is my opinion, the call should be to the package owner.
However, it is not a huge problem, because explained_variance_ratio_ is generally not used in calculations, but more as an "explanation" :) of what vectors were chosen in the dimension reduction.
With the svd solver, explained_variance_ratio will have a length of maximum n_classes (3 here). Three classes possible because of this line:
y = state.randint(0, 3, size=(40,))
explained_variance_ratio_ should be of length 5, because we asked for 5 components.
Also, the test function for the attribute is biased:
deftest_lda_explained_variance_ratio():
# Test if the sum of the normalized eigen vectors values equals 1,# Also tests whether the explained_variance_ratio_ formed by the# eigen solver is the same as the explained_variance_ratio_ formed# by the svd solverstate=np.random.RandomState(0)
X=state.normal(loc=0, scale=100, size=(40, 20))
y=state.randint(0, 3, size=(40,))
clf_lda_eigen=LinearDiscriminantAnalysis(solver="eigen", n_components=5)
clf_lda_eigen.fit(X, y)
assert_almost_equal(clf_lda_eigen.explained_variance_ratio_.sum(), 1.0, 3)
clf_lda_svd=LinearDiscriminantAnalysis(solver="svd", n_components=7)
clf_lda_svd.fit(X, y)
assert_almost_equal(clf_lda_svd.explained_variance_ratio_.sum(), 1.0, 3)
# print(clf_lda_svd.explained_variance_ratio_.shape[0]) -> 3 !!!tested_length=min(clf_lda_svd.explained_variance_ratio_.shape[0],
clf_lda_eigen.explained_variance_ratio_.shape[0])
# print(tested_length) -> 3 !!!# NOTE: clf_lda_eigen.explained_variance_ratio_ is not of n_components# length. Make it the same length as clf_lda_svd.explained_variance_ratio_# before comparison.assert_array_almost_equal(clf_lda_svd.explained_variance_ratio_,
clf_lda_eigen.explained_variance_ratio_[:tested_length])
Activity
JPFrancoia commentedon Dec 15, 2015
Yep sorry, this is my fault, as I said in #5216 , explained_variance_ratio_ should have a length of n_components.
the code should be:
But this is an API change.
hlin117 commentedon Dec 15, 2015
@JPFrancoia: It might be an API change, or a documentation change.
I'm concerned because there are two routes we can go from here:
explained_variance_ratio_is not of lengthn_componentsI'm waiting for the package owners to help guide this issue, because each one has consequences. Nonetheless, this PR should be addressed in some form.
JPFrancoia commentedon Dec 15, 2015
Yep I agree.
The thing is,
explained_variance_ratio_is lengthn_componentsfor the svd solver, but not for the eigen solver. So changing the doc would be problematic.I think the "less worse" solution is to go for the code. But this is my opinion, the call should be to the package owner.
However, it is not a huge problem, because explained_variance_ratio_ is generally not used in calculations, but more as an "explanation" :) of what vectors were chosen in the dimension reduction.
amueller commentedon Oct 7, 2016
I vote this is a bugfix and the change is ok. Was the attribute introduced in 0.18 or before?
JPFrancoia commentedon Oct 7, 2016
It was merged in master on the 11 Sep 2015. Before 0.18, it's already present in the 0.17.1
amueller commentedon Oct 8, 2016
that's too bad. I still would probably change it.
JPFrancoia commentedon Oct 8, 2016
Shall I open a PR ?
amueller commentedon Oct 8, 2016
@JPFrancoia go for it :)
JPFrancoia commentedon Oct 8, 2016
The PR will fix the issue. However we still have a problem:
explained_variance_ratio_, for the svd solver, does not have the right length either !With the svd solver,
explained_variance_ratiowill have a length of maximum n_classes (3 here). Three classes possible because of this line:explained_variance_ratio_should be of length 5, because we asked for 5 components.Also, the test function for the attribute is biased:
amueller commentedon Oct 8, 2016
n_components an be at most n_classes in LDA.
JPFrancoia commentedon Oct 9, 2016
Ok, I updated my PR then. It should be ok now. I also updated the non regression test.
Fix issue scikit-learn#6032. Attribute explained_variance_ratio_ from
mohamed-ali commentedon Feb 26, 2018
@jnothman @amueller, this issue is marked as closed in here, after being fixed with the PR #7632, however, it's in the list of issues without PR in here: https://github.com/scikit-learn/scikit-learn/projects/5.
I found that accidentally while looking for issues to work on.
jnothman commentedon Feb 26, 2018