-
-
Notifications
You must be signed in to change notification settings - Fork 26k
[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
Conversation
I would have thought that |
@glemaitre Yea I like that solution better as well, will implement it. But note that then |
sklearn/linear_model/bayes.py
Outdated
@@ -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) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
# constant target vectors | ||
n_samples = 4 | ||
n_features = 5 | ||
constant_value = np.random.rand() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use a RandomState
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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.
thx @jdoepfert |
awesome! |
… 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
… 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
Reference Issues/PRs
Fixes #10092
What does this implement/fix? Explain your changes.
This PR fixes the issue that when fitting a
BayesianRidge
orARDRegression
classifier with a constant target vectory
, thepredict
method yields NaN arrays for both predictions and the respective standard devitions. This occurs since the estimated precision for the noisealpha_
is initialized with1/np.var(y)
=inf
.This PR fixes this issue by initializing
alpha_
with1/(np.var(y) + eps)
, whereeps
=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: