-
-
Notifications
You must be signed in to change notification settings - Fork 25.8k
[MRG+1] remove n_nonzero_coefs from attr of LassoLarsCV + clean up call hierarchy #9004
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
[MRG+1] remove n_nonzero_coefs from attr of LassoLarsCV + clean up call hierarchy #9004
Conversation
@@ -370,11 +370,11 @@ def lars_path(X, y, Xy=None, Gram=None, max_iter=500, | |||
corr_eq_dir = np.dot(Gram[:n_active, n_active:].T, | |||
least_squares) | |||
|
|||
g1 = arrayfuncs.min_pos((C - Cov) / (AA - corr_eq_dir + tiny)) | |||
g1 = arrayfuncs.min_pos((C - Cov) / (AA - corr_eq_dir + tiny32)) |
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.
tiny was not enough for #8475
and I think that 1.1754944e-38 is small enough (rather than 2.2250738585072014e-308)
alpha = 0. # n_nonzero_coefs parametrization takes priority | ||
max_iter = self.n_nonzero_coefs | ||
else: | ||
max_iter = self.max_iter |
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.
this has been moved to Lars.fit to avoid the magic of hacking instance attributes
sklearn/linear_model/least_angle.py
Outdated
@@ -1145,10 +1154,14 @@ def fit(self, X, y): | |||
# Now compute the full model | |||
# it will call a lasso internally when self if LassoLarsCV | |||
# as self.method == 'lasso' | |||
Lars.fit(self, X, y) | |||
Lars._fit(self, X, y, max_iter=self.max_iter, alpha=best_alpha, |
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.
now self.max_iter is not ignored
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.
Is it the case that previously the call to Lars.fit reverted to a call to lasso_lars with alpha_min=0
, while now alpha_min=best_alpha
?
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.
yes
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.
great! so we save a bit of computation that we didn't need to do previously.
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.
Should we maybe just say self._fit
now?
sklearn/linear_model/least_angle.py
Outdated
@property | ||
@deprecated("Attribute alpha is deprecated in 0.18 and " | ||
"will be removed in 0.20. See 'alpha_' instead") |
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.
this was a hack to be able to call Lars.fit but the CV classes should have an alpha_ but no alpha param
@@ -402,6 +403,7 @@ def test_lars_cv(): | |||
lars_cv.fit(X, y) | |||
np.testing.assert_array_less(old_alpha, lars_cv.alpha_) | |||
old_alpha = lars_cv.alpha_ | |||
assert_false(hasattr(lars_cv, 'n_nonzero_coefs')) |
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.
maybe a test for the ignored max_iter?
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
@@ -195,7 +195,6 @@ def lars_path(X, y, Xy=None, Gram=None, max_iter=500, | |||
sys.stdout.write('.') | |||
sys.stdout.flush() | |||
|
|||
tiny = np.finfo(np.float).tiny # to avoid division by 0 warning | |||
tiny32 = np.finfo(np.float32).tiny # to avoid division by 0 warning |
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.
for the record tiny32 was introduced here 294d4b6
Codecov Report
@@ Coverage Diff @@
## master #9004 +/- ##
==========================================
+ Coverage 95.91% 95.92% +<.01%
==========================================
Files 331 331
Lines 59851 59960 +109
==========================================
+ Hits 57409 57516 +107
- Misses 2442 2444 +2
Continue to review full report at Codecov.
|
ea66a5b
to
fde74da
Compare
good to go on my end appveyor error is an appveyor outage so is unrelated |
sklearn/linear_model/least_angle.py
Outdated
""" | ||
X, y = check_X_y(X, y, y_numeric=True, multi_output=True) | ||
def _fit(self, X, y, max_iter, alpha, fit_path, Xy=None): | ||
"""Aux method to fit the model using X, y as training data""" |
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 know it's private, but I think it should still say "Auxiliary" :P
Other than the two extremely minor comments this LGTM. The regression test indeed fails on master and passes here. Clear improvement in terms of the code. |
002c95a
to
70338a0
Compare
self.copy_X = copy_X | ||
self.positive = positive | ||
# XXX : we don't use super(LarsCV, self).__init__ | ||
# to avoid setting n_nonzero_coefs |
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.
Technically it tells us that we have the wrong inheritance diagram. But... I don't bother.
LGTM. Merging |
…ll hierarchy (scikit-learn#9004) * FIX : remove n_nonzero_coefs from attr of LassoLarsCV + clean up call to Lars._fit * cleanup * fix deprecation warning + clarify warning * add test * pep8 * adddress comments
…ll hierarchy (scikit-learn#9004) * FIX : remove n_nonzero_coefs from attr of LassoLarsCV + clean up call to Lars._fit * cleanup * fix deprecation warning + clarify warning * add test * pep8 * adddress comments
…ll hierarchy (scikit-learn#9004) * FIX : remove n_nonzero_coefs from attr of LassoLarsCV + clean up call to Lars._fit * cleanup * fix deprecation warning + clarify warning * add test * pep8 * adddress comments
…ll hierarchy (scikit-learn#9004) * FIX : remove n_nonzero_coefs from attr of LassoLarsCV + clean up call to Lars._fit * cleanup * fix deprecation warning + clarify warning * add test * pep8 * adddress comments
…ll hierarchy (scikit-learn#9004) * FIX : remove n_nonzero_coefs from attr of LassoLarsCV + clean up call to Lars._fit * cleanup * fix deprecation warning + clarify warning * add test * pep8 * adddress comments
…ll hierarchy (scikit-learn#9004) * FIX : remove n_nonzero_coefs from attr of LassoLarsCV + clean up call to Lars._fit * cleanup * fix deprecation warning + clarify warning * add test * pep8 * adddress comments
…ll hierarchy (scikit-learn#9004) * FIX : remove n_nonzero_coefs from attr of LassoLarsCV + clean up call to Lars._fit * cleanup * fix deprecation warning + clarify warning * add test * pep8 * adddress comments
…ll hierarchy (scikit-learn#9004) * FIX : remove n_nonzero_coefs from attr of LassoLarsCV + clean up call to Lars._fit * cleanup * fix deprecation warning + clarify warning * add test * pep8 * adddress comments
Reference Issue
Fixes #8475
What does this implement/fix? Explain your changes.
introduce a private _fit method in Lars to fit with explicit params without
having to hack the instance attributes.
It also fixes a call to Lars.fit that was ignoring the max_iter parameter.
Any other comments?
Nope