python - 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
积极的)
我应该怎么做才能修复这些警告或阻止它们?
解决方案
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)
推荐阅读
- python - 获取类“属性方法”上的属性集
- javascript - 有人可以告诉我为什么我的代码正在运行。即使我已经评论了我的变量用户名,电子邮件,密码,密码2
- r - 在两列中识别相同的项目?
- flutter - 是否可以在 Flutter 中创建声音编辑器?
- c# - 使用 process.start 从 .netcore 运行 docker 会引发错误
- html - 用于修改加载网页图像的 Chrome 扩展程序
- python - 类型错误:* 之后的 add() 参数必须是可迭代的,而不是设置
- macos - Ace 编辑器行相互重叠
- python - 从列中提取子字符串
- php - 带有现成物品和发票的 PayPal 订阅 API