You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Lasso回归搭配GridSearchCV出现收敛警告的解决方法咨询

Lasso回归结合GridSearchCV调优收敛警告解决方案
  • 提升最大迭代次数
    Lasso默认的max_iter参数值为1000,当正则系数alpha较小、数据集特征规模大时,坐标下降法迭代1000次达不到收敛阈值,直接调大该参数即可,比如设置为10000甚至更高:
    lasso = Lasso(normalize=True, tol=0.0001, max_iter=10000)
    
  • 调整收敛容忍度tol
    当前你设置的tol=0.0001阈值较低,可以适当上调到0.001或0.01,容忍更小的对偶间隙即可判定收敛,注意不要调整过大避免影响模型精度。
  • 替换弃用的normalize参数,手动做特征标准化
    scikit-learn 1.2及以上版本已经废弃了Lasso类的normalize参数,内部标准化的实现逻辑容易引发收敛异常,更稳妥的做法是先用StandardScaler对训练集特征做标准化,再传入模型训练,同时去掉normalize=True的设置。
  • 仅过滤警告(不推荐优先使用)
    如果你确认当前未收敛的模型结果精度已经满足需求,可以手动过滤该类警告,在代码开头加入以下内容:
    import warnings
    from sklearn.exceptions import ConvergenceWarning
    warnings.filterwarnings("ignore", category=ConvergenceWarning)
    

修改后的完整参考代码:

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

# 先标准化特征
scaler = StandardScaler()
X_tr_scaled = scaler.fit_transform(X_tr)

alpha_space = {'alpha': np.logspace(-4, 0, 50)}
# 去掉normalize参数,加大迭代次数
lasso = Lasso(tol=0.0001, max_iter=10000)
grid_search_lr = GridSearchCV(lasso, alpha_space, cv=3, scoring="neg_mean_squared_error")
grid_search_lr.fit(X_tr_scaled, y_tr)

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

内容的提问来源于stack exchange,提问作者Jared Persad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 02:27:02