Repository navigation
[WIP] Storing the best attributes of (non-GridSearch) CV models #5498
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1034,9 +1034,18 @@ class LarsCV(Lars): | |
| n_iter_ : array-like or int | ||
| the number of iterations run by Lars with the optimal alpha. | ||
|
|
||
| ``best_score_`` : float | ||
| Best cross validation score on the left out data | ||
| (i.e. best mean squared error). Note that by default GridSearchCV | ||
| computes ``best_score_`` based on the R^2, not the mean squared error, | ||
| which will account for differences between the two. | ||
|
|
||
| ``best_params_`` : dict | ||
| Parameter setting that gave the best results on the left out data. | ||
|
|
||
| See also | ||
| -------- | ||
| lars_path, LassoLars, LassoLarsCV | ||
| lars_path, LassoLars, LassoLarsCV, Lasso, LassoCV | ||
| """ | ||
|
|
||
| method = 'lar' | ||
|
|
@@ -1119,11 +1128,14 @@ def fit(self, X, y): | |
| # Select the alpha that minimizes left-out error | ||
| i_best_alpha = np.argmin(mse_path.mean(axis=-1)) | ||
| best_alpha = all_alphas[i_best_alpha] | ||
| best_mse = mse_path[i_best_alpha] | ||
|
|
||
| # Store our parameters | ||
| self.alpha_ = best_alpha | ||
| self.cv_alphas_ = all_alphas | ||
| self.cv_mse_path_ = mse_path | ||
| self.best_score_ = best_mse | ||
| self.best_params_ = {'alpha': best_alpha} | ||
|
|
||
| # Now compute the full model | ||
| # it will call a lasso internally when self if LassoLarsCV | ||
|
|
@@ -1235,6 +1247,15 @@ class LassoLarsCV(LarsCV): | |
| n_iter_ : array-like or int | ||
| the number of iterations run by Lars with the optimal alpha. | ||
|
|
||
| ``best_score_`` : float | ||
| Best cross validation score on the left out data | ||
| (i.e. best mean squared error). Note that by default GridSearchCV | ||
| computes ``best_score_`` based on the R^2, not the mean squared error, | ||
| which will account for differences between the two. | ||
|
|
||
| ``best_params_`` : dict | ||
| Parameter setting that gave the best results on the left out data. | ||
|
|
||
| Notes | ||
| ----- | ||
|
|
||
|
|
@@ -1338,6 +1359,15 @@ class LassoLarsIC(LassoLars): | |
| alphas. The alpha which has the smallest information criteria | ||
| is chosen. | ||
|
|
||
| ``best_score_`` : float | ||
| Best cross validation score on the left out data | ||
| (i.e. best mean squared error). Note that by default GridSearchCV | ||
| computes ``best_score_`` based on the R^2, not the mean squared error, | ||
| which will account for differences between the two. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't like the last part of the sentence. The difference between which two? GridSearchCV with a different score? I would just remove the "which will accout" part.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [on all three docstrings] |
||
|
|
||
| ``best_params_`` : dict | ||
| Parameter setting that gave the best results on the left out data. | ||
|
|
||
| Examples | ||
| -------- | ||
| >>> from sklearn import linear_model | ||
|
|
@@ -1441,4 +1471,6 @@ def fit(self, X, y, copy_X=True): | |
| self.alpha_ = alphas_[n_best] | ||
| self.coef_ = coef_path_[:, n_best] | ||
| self._set_intercept(Xmean, ymean, Xstd) | ||
| self.best_score_ = mean_squared_error | ||
| self.best_params_ = {'alpha': self.alpha_} | ||
| return self | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,10 @@ | |
| from sklearn.utils.testing import assert_array_equal | ||
| from sklearn.utils.testing import TempMemmap | ||
|
|
||
| from sklearn.cross_validation import KFold | ||
|
|
||
| from sklearn.grid_search import GridSearchCV | ||
|
|
||
| from sklearn.linear_model.coordinate_descent import Lasso, \ | ||
| LassoCV, ElasticNet, ElasticNetCV, MultiTaskLasso, MultiTaskElasticNet, \ | ||
| MultiTaskElasticNetCV, MultiTaskLassoCV, lasso_path, enet_path | ||
|
|
@@ -674,3 +678,35 @@ def test_lasso_non_float_y(): | |
| clf_float = model(fit_intercept=False) | ||
| clf_float.fit(X, y_float) | ||
| assert_array_equal(clf.coef_, clf_float.coef_) | ||
|
|
||
|
|
||
| def test_lasso_cv_best_score(): | ||
| X, y, X_test, y_test = build_dataset(n_features=20) | ||
| grid_cv = GridSearchCV( | ||
| Lasso(), {'alpha': [0.1, 1., 10.]}, scoring='mean_squared_error', | ||
| cv=KFold(3) | ||
| ).fit(X, y) | ||
| lasso_cv = LassoCV(alphas=[0.1, 1., 10.], cv=KFold(3)).fit(X, y) | ||
| assert_equal(lasso_cv.best_score_, -1 * grid_cv.best_score_) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the -1 makes me die a little inside.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You have to become a bit more pragmatic
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't find user-suprising behaviors to be pragmatic.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't find user-suprising behaviors to be pragmatic.
OK, I miss read the line above (the line of code) and said something
stupid.
I agree that it is really nasty.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @amueller simply awful, I totally agree with you.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At the risk of not looking at the source code before hand, how does gridsearch know if its the error or accuracy?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is a sort-of long-standing api issue... #5023 we're gonna decide this sprint which way to go I think.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did not read the entire issue yet, but MLlib solves it by having an explicit flag, apache/spark#8290
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah they introduced that in the last release. That was the way I initially did it, then we changed it to flipping the sign. We'll have a discussion tomorrow. |
||
|
|
||
|
|
||
| def test_elasticnet_cv_best_score(): | ||
| X, y, X_test, y_test = build_dataset(n_features=20) | ||
| grid_cv = GridSearchCV( | ||
| ElasticNet(), | ||
| {'alpha': [0.1, 1., 10.], 'l1_ratio': [0.1, 0.5, 0.9, 0.95, 0.99]}, | ||
| scoring='mean_squared_error', cv=KFold(3) | ||
| ).fit(X, y) | ||
| enet_cv = ElasticNetCV(alphas=[0.1, 1., 10.], cv=KFold(3)).fit(X, y) | ||
| assert_equal(enet_cv.best_score_, -1 * grid_cv.best_score_) | ||
|
|
||
|
|
||
| def test_lasso_cv_l1ratio(): | ||
| X, y, X_test, y_test = build_dataset(n_features=20) | ||
| lasso_cv = LassoCV().fit(X, y) | ||
| assert_equal(lasso_cv.best_params_['l1_ratio'], 1) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| import nose | ||
| nose.runmodule() | ||
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 think we don't need the ticks here any more.
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.
@amueller More detail on this, please ?
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 suppose that Andreas Mueller should answer this but it seems what he meant is the ticks `` are not needed. It is to bold the text., but now it is done by default for the attributes. Hope it helps
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.
@maniteja123 , yeah. Made sense. thanks!