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
Precision errors in LDA.explained_variance_ratio_ #6031
This surfaces the discussion in #5216. Here are some precision differences than @JPFrancoia and I are looking at:
importnumpyasnpfromsklearn.discriminant_analysisimportLinearDiscriminantAnalysisfromsklearn.utils.testingimportassert_array_almost_equalstate=np.random.RandomState(0)
X=state.normal(loc=0, scale=100, size=(40, 20))
y=state.randint(0, 3, size=(40, 1))
# Train the LDA classifier. Use the eigen solverlda_eigen=LDA(solver='eigen', n_components=5)
lda_eigen.fit(X, y)
lda_svd=LDA(n_components=5, solver='svd')
lda_svd.fit(X, y)
assert_array_almost_equal(lda_eigen.explained_variance_ratio_, lda_svd.explained_variance_ratio_)
AssertionError:
Arrays are not almost equal to 6 decimals
(shapes (20,), (3,) mismatch)
x: array([ 6.03795532e-01, 3.96204468e-01, 5.85621882e-16,
3.18609950e-16, 2.08378911e-16, 1.21510637e-16,
7.83079028e-17, 7.58612317e-17, 3.89040436e-17,...
y: array([ 5.52469269e-01, 4.47530731e-01, 7.08925911e-17])
Besides the fact that lda_eigen has the wrong number of components for the explained_variance_ratio_ (according to the docs, it should have only n_components=5), there are numeric differences as well.
@mbillingr , actually, the numerical differences are due to the non squared S matrix. With your suggestion in #5216 , we solved it. Now we square this matrix and it's all right. See #6027 (comment)
Also, lda_svd should have 5 components here. It is just a typo here I think:
Activity
mbillingr commentedon Dec 17, 2015
Do you still see these differences with data where
n_samples >> n_features?Btw, should
lda_svdhave five components instead of three?JPFrancoia commentedon Dec 17, 2015
@mbillingr , actually, the numerical differences are due to the non squared S matrix. With your suggestion in #5216 , we solved it. Now we square this matrix and it's all right. See #6027 (comment)
Also,
lda_svdshould have 5 components here. It is just a typo here I think:If you use 5 instead of 3, it's all right.
mbillingr commentedon Dec 17, 2015
Oh, thank you. I missed this in the discussion of the PR and thought there was still a difference after squaring S :)
amueller commentedon Oct 7, 2016
The numeric issues seem to be fixed in master but the
explained_variance_ratio_still have different sized :(JPFrancoia commentedon Oct 7, 2016
Yes, this is because of #6032 . The problem is well defined, but fixing the bug is an API change.
amueller commentedon Oct 7, 2016
Ok closing this as duplicate of #6032 then