首页 > 解决方案 > Lasso 回归模型与 GridSearchCV 有收敛警告

问题描述

这是我拥有的代码:

from sklearn.linear_model import Lasso
from sklearn.model_selection import GridSearchCV
import numpy as np

alpha_space = {'alpha': np.logspace(-4, 0, 50)}
lasso = Lasso(normalize=True,  tol=0.0001)
grid_search_lr = GridSearchCV (lasso, alpha_space, cv=3, scoring="neg_mean_squared_error")
grid_search_lr.fit(X_tr, y_tr)

print(grid_search_lr.best_params_)
print(np.sqrt(-grid_search_lr.best_score_))

但是当我去运行它时,在答案之前我至少收到了 20 个这样的警告:

/usr/local/lib/python3.7/dist-packages/sklearn/linear_model/_coordinate_descent.py:476: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 8451216620580.201, tolerance: 12888767617.309622

积极的)

我应该怎么做才能修复这些警告或阻止它们?

标签: pythonmachine-learningscikit-learngridsearchcvlasso-regression

解决方案


The Lasso estimator uses an iterative algorithm to solve the optimization problem. The iterative algorithm stops when it reaches the required level of convergence (set with the tolerance tol). To avoid having the algorithm perform too many iterations (and possibly never stop), the algorithm also stops when it has performed a maximum number of iteration (max_iter). In this case, it raises a warning that it failed to reach the required level of convergence.

To avoid the convergence warning, you can either:

  • increase the tolerance tol (to require a less strict convergence level)
  • increase the maximum number of iteration max_iter (to spend more time finding the convergence level)

推荐阅读