8000 [MRG+2] Fix LDA predict_proba() by agamemnonc · Pull Request #11796 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

[MRG+2] Fix LDA predict_proba() #11796

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 26 commits into from
Mar 7, 2019
Merged

[MRG+2] Fix LDA predict_proba() #11796

merged 26 commits into from
Mar 7, 2019

Conversation

agamemnonc
Copy link
Contributor
@agamemnonc agamemnonc commented Aug 11, 2018

Reference Issues/PRs

Fixes #6848
closes #11727
closes #5149

What does this implement/fix? Explain your changes.

Fixes the predict_proba() method of LinearDiscriminantAnalysis.
An if statement is used to differentiate between the binary and multi-class case, due to the different output format of the decision_function method implemented in the LinearClassifierMixin class.

Any other comments?

Copying from #6848:
Do we perhaps want to include additional tests checking the output of predict_proba for LDA and QDA both for the binary and multi-class cases?

@agamemnonc agamemnonc changed the title Fix LDA predict_proba() [WIP] Fix LDA predict_proba() Aug 11, 2018
Copy link
Member
@jnothman jnothman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this needs non-regression tests

@agamemnonc
Copy link
Contributor Author

@jnothman do you mean a numerical test that checks that the output probabilities of a toy dataset are as expected (e.g. something similar to test_qda_store_covariance() in test_discriminant_analysis.py)?

@jnothman
Copy link
Member
jnothman commented Aug 20, 2018 via email

@agamemnonc
Copy link
Contributor Author
agamemnonc commented Aug 21, 2018

Could you please let me know if the non-regression test looks OK?
If so, I will change the prefix to MRG.

@agamemnonc agamemnonc changed the title [WIP] Fix LDA predict_proba() [MRG] Fix LDA predict_proba() Aug 24, 2018
@agamemnonc
Copy link
Contributor Author

Can I suggest that this PR be prioritised given that it fixes a bug (#6848), which yields wrong prediction outcomes for a somehow popular classifier?

@amueller amueller added the Bug label Oct 22, 2018
@taalexander
Copy link

Hello, I just would like to note that I am running into this bug in practice and would really appreciate a fix.

@jnothman
Copy link
Member
jnothman commented Dec 3, 2018

Thanks for the pings, @agamemnonc, @taalexander. I'll look soon.

Copy link
Member
@jnothman jnothman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This LGTM. I only wonder whether this should be encapsulated in a logistic utility function.

# up to a multipl 8000 icative constant.
likelihood = np.exp(prob - prob.max(axis=1)[:, np.newaxis])
# compute posterior probabilities
return likelihood / likelihood.sum(axis=1)[:, np.newaxis]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not continue to do this inplace (/=)?

@agamemnonc
Copy link
Contributor Author
agamemnonc commented Dec 5, 2018

Thanks @jnothman .

Yes, you are right. Hopefully the most recent commit is much cleaner; it is also consistent with the predict_proba method of LogisticRegression.

I have also fixed a typo in test_discriminant_analysis.py.

@jnothman
Copy link
Member
jnothman commented Dec 9, 2018

Nice!

@agamemnonc
Copy link
Contributor Author

Given the already merged #12931, now there is a a conflict. (ΒΤW, I believe this could have been avoided if this PR had been timely merged (PR submitted on December 5th, whereas #12931 submitted on January 6th)).

Anyway, I suggest that the changes in the most recent commit are overwritten by the current PR , since the code in this PR inherits the method from the parent class (LinearClassifierMixin) rather than re-implementing it.

@jnothman
Copy link
Member

Apologies about the poor management of related pull requests on our part. Please resolve conflicts with master so we can see the benefits of this pr more clearly

@agamemnonc
Copy link
Contributor Author
agamemnonc commented Jan 15, 2019

OK, no problem.

I have now provided a fix, since the test introduced in this PR (test_lda_predict_proba) previously failed.

Moreover, the suggested fix reuses code by inheriting from the parent class rather than re-implementing the method when n_classes == 2.

Some checks have failed, not sure why. This was previously due to a missing check_is_fitted check and has been now fixed.

jnothman
jnothman previously approved these changes Jan 16, 2019
@jnothman
Copy link
Member

Please add a |Fix| entry to the change log at doc/whats_new/v0.21.rst. Like the other entries there, please reference this pull request with :issue: and credit yourself (and other contributors if applicable) with :user:

@agamemnonc
8000 Copy link
Contributor Author

Please add a |Fix| entry to the change log at doc/whats_new/v0.21.rst. Like the other entries there, please reference this pull request with :issue: and credit yourself (and other contributors if applicable) with :user:

Done, thanks for the instructions.

@jnothman
Copy link
Member

It would be good to get this in 0.21. Thanks @agamemnonc if you can get to it.

@agamemnonc
Copy link
Contributor Author

Apologies for the delay, I have been very busy with a submission recently, will try to deal with this by the end of this week.

@jnothman
Copy link
Member
jnothman commented Feb 28, 2019 via email

@agamemnonc
Copy link
Contributor Author

OK folks, I think I have now implemented everything we have agreed on.

If tests pass, @agramfort, @jnothman, @glemaitre could you please have a final look and merge if happy or let me know otherwise.

Copy link
Member
@glemaitre glemaitre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@agamemnonc agamemnonc changed the title [MRG+1] Fix LDA predict_proba() [MRG+2] Fix LDA predict_proba() Mar 1, 2019
n_samples=90000, centers=blob_centers, covariances=blob_stds,
random_state=42
)
lda = LinearDiscriminantAnalysis(solver='lsqr').fit(X, y)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we want to test this for the other solvers as well? how long does the test take given the amount of samples?

Copy link
Contributor Author
@agamemnonc agamemnonc Mar 1, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point.

Including the two other solvers adds only 0.09 s.

The test passes for solver=svd, but fails when solver=eigen.
This is probably related to #11727.

@amueller Shall we only include svd and lsqr for now in the tests and take a note in that other PR to update the tests to also include eigen when a fix is submitted?

I will try to also provide a fix for the eigen solver in this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I think I have now fixed that other issue which was due to bad normalisation of the eigenvectors and was causing issues with probabilities for the eigen solver #11727 . I have updated the rst file accordingly.

Now all three solvers are tested in the non-regression test.

@agamemnonc
Copy link
Contributor Author

@glemaitre @jnothman you might need to re-approve, as I have modified the code in _solve_eigen. This was causing issues with the probabilities when using the eigen method (cf. #11727) --I am not sure why this normalisation was introduced there.

Copy link
Member
@agramfort agramfort left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

besides my nitpick LGTM

@agramfort agramfort merged commit 4140657 into scikit-learn:master Mar 7, 2019
@agramfort
Copy link
Member

thx @agamemnonc

@agamemnonc agamemnonc deleted the lda_predict_proba_fix branch March 7, 2019 17:00
@glemaitre
Copy link
Member

A bit late but thanks a lot @agamemnonc

@agamemnonc
Copy link
Contributor Author

A bit late but thanks a lot @agamemnonc

My pleasure—thank you all for all your help and feedback.

xhluca pushed a commit to xhluca/scikit-learn that referenced this pull request Apr 28, 2019
* fix LDA predict_proba() to handle binary and multi-class case

* test_lda_predict_proba non-regression test

* pep8 fix

* lda predict_proba refactoring

* Typo fix

* flake8 fix

* predict_proba check_is_fitted check

* update what's new rst file

* rename prob to decision

* include additional tests for predict_proba

* use allcose vs. assert_array_almost_equal

* fix indent

* replace len with size

* explicit computation for binary case

* fix style whats_new rst

* predict_proba new regression test

* give credit for regression test

* fix bug for eigen solution

* include all three solvers in predict_proba regression test

*  update whats_new rst file

* fix minor formatting issue

* use scipy.linalg instead of np.linalg
xhluca pushed a commit to xhluca/scikit-learn that referenced this pull request Apr 28, 2019
xhluca pushed a commit to xhluca/scikit-learn that referenced this pull request Apr 28, 2019
koenvandevelde pushed a commit to koenvandevelde/scikit-learn that referenced this pull request Jul 12, 2019
* fix LDA predict_proba() to handle binary and multi-class case

* test_lda_predict_proba non-regression test

* pep8 fix

* lda predict_proba refactoring

* Typo fix

* flake8 fix

* predict_proba check_is_fitted check

* update what's new rst file

* rename prob to decision

* include additional tests for predict_proba

* use allcose vs. assert_array_almost_equal

* fix indent

* replace len with size

* explicit computation for binary case

* fix style whats_new rst

* predict_proba new regression test

* give credit for regression test

* fix bug for eigen solution

* include all three solvers in predict_proba regression test

*  update whats_new rst file

* fix minor formatting issue

* use scipy.linalg instead of np.linalg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
7 participants
0