Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[MRG + 1] Fix BayesianRidge() and ARDRegression() for constant target vectors #10095

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 17 commits into from
Nov 10, 2017

Conversation

jdoepfert
Copy link
Contributor

@jdoepfert jdoepfert commented Nov 9, 2017

Reference Issues/PRs

Fixes #10092

What does this implement/fix? Explain your changes.

This PR fixes the issue that when fitting a BayesianRidge or ARDRegression classifier with a constant target vector y, the predict method yields NaN arrays for both predictions and the respective standard devitions. This occurs since the estimated precision for the noise alpha_ is initialized with 1/np.var(y) = inf.

This PR fixes this issue by initializing alpha_ with 1/(np.var(y) + eps), where eps = np.spacing(1).

Any other comments?

The returned standard deviation std for the predictions will not be zero for a constant target vector (as suggested in #10092), but a small number instead. I added a test for this. However, std approaches zero as the number of samples increase, as expected:
image

@glemaitre
Copy link
Member

I would have thought that alpha_ = 1. / (np.var(y) + np.spacing(1)) should be enough

@jdoepfert jdoepfert changed the title [MRG] Fix BayesianRidge() for constant target vectors [WIP] Fix BayesianRidge() for constant target vectors Nov 9, 2017
@jdoepfert
Copy link
Contributor Author

@glemaitre Yea I like that solution better as well, will implement it. But note that then std will not be zero, as suggested in the issue.

@@ -162,7 +162,8 @@ def fit(self, X, y):
n_samples, n_features = X.shape

# Initialization of the values of the parameters
alpha_ = 1. / np.var(y)
eps = np.spacing(1)
Copy link
Member

Choose a reason for hiding this comment

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

You might want to add a small comment to explain why we add eps

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thx for the suggestion. I added a comment, and additionally applied the same fix to ARDRegression

Copy link
Member

Choose a reason for hiding this comment

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

I would prefer you use:

eps = np.finfo(np.float64).eps

as here you're not taking into account the dtype of X or y anyway

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@jdoepfert jdoepfert changed the title [WIP] Fix BayesianRidge() for constant target vectors [WIP] Fix BayesianRidge() and ARDRegression() for constant target vectors Nov 9, 2017
@jdoepfert jdoepfert changed the title [WIP] Fix BayesianRidge() and ARDRegression() for constant target vectors [MRG] Fix BayesianRidge() and ARDRegression() for constant target vectors Nov 9, 2017
# constant target vectors
n_samples = 4
n_features = 5
constant_value = np.random.rand()
Copy link
Member

Choose a reason for hiding this comment

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

use a RandomState

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

# The standard dev. should be relatively small (< 0.1 is tested here)
n_samples = 4
n_features = 5
constant_value = np.random.rand()
Copy link
Member

Choose a reason for hiding this comment

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

idem use RandomState


def test_std_ard_with_constant_input():
# Test ARDRegression standard dev. for edge case of constant target vector
# The standard dev. should be relatively small (< 0.1 is tested here)
Copy link
Member

Choose a reason for hiding this comment

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

That seems quite a large standard dev...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

tl;dr: I decreased the upper bound to 0.01.

Playing around with a couple of random initializations, the std was around 0.005. To make sure that the test won't fail by chance, I originally chose 0.1 as an upper bound. Now that I use a random state in the test, it's safe to reduce the bound to 0.01.

@glemaitre glemaitre changed the title [MRG] Fix BayesianRidge() and ARDRegression() for constant target vectors [MRG + 1] Fix BayesianRidge() and ARDRegression() for constant target vectors Nov 10, 2017
@agramfort agramfort merged commit c6005e1 into scikit-learn:master Nov 10, 2017
@agramfort
Copy link
Member

thx @jdoepfert

@jdoepfert
Copy link
Contributor Author

awesome!

maskani-moh pushed a commit to maskani-moh/scikit-learn that referenced this pull request Nov 15, 2017
… vectors (scikit-learn#10095)

* add test for issue scikit-learn#10092

* add comment to test

* split into two tests

* add tests for scores, alpha and beta

* adapt tests: n_samples != n_features

* add test when no intercept is fitted

* add handling of constant target vector when intercept is fitted

* fix typo in comments

* fix format issues

* replace original fix with simpler fix

* add comment

* increase upper boundary for test

* increase upper boundary for test

* merge tests for ARDRegression and BayesianRidge

* use random state in tests

* decrease upper bound for std

* replace np.spacing(1) -> np.finfo(np.float64).eps
jwjohnson314 pushed a commit to jwjohnson314/scikit-learn that referenced this pull request Dec 18, 2017
… vectors (scikit-learn#10095)

* add test for issue scikit-learn#10092

* add comment to test

* split into two tests

* add tests for scores, alpha and beta

* adapt tests: n_samples != n_features

* add test when no intercept is fitted

* add handling of constant target vector when intercept is fitted

* fix typo in comments

* fix format issues

* replace original fix with simpler fix

* add comment

* increase upper boundary for test

* increase upper boundary for test

* merge tests for ARDRegression and BayesianRidge

* use random state in tests

* decrease upper bound for std

* replace np.spacing(1) -> np.finfo(np.float64).eps
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BayesRidge can returned some NaN prediction and std
4 participants