首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Python使用逻辑回归提示FutureWarning:Specify a solver to silence warning

在Python中利用Logistic回归算法进行数据建模,本来算是比较常见的事情,但结果“阴沟里翻船”,一上来就遇到了报警提示。

在PyCharm中,使用python的sklearn.linear_model.LogisticRegression进行实例化时model=LogisticRegression(),就提示了以下警告信息:

FutureWarning: Default solver will be changed to 'lbfgs' in 0.22. Specify a solver to silence this warning。

虽然警告信息并不影响代码运行,但输出窗口异常明显的几行红字提醒,我总觉得代码的心里也不会很爽快。

问题分析:

FutureWarning是语言或者库中将来可能改变的有关警告。

根据报警信息和参考相关文档,“Default will change from 'liblinear' to 'lbfgs' in 0.22.”,默认的solver参数在0.22版本中,将会由“liblinear”变为“lbfgs”,且指定solver参数可以消除该warning。

这是代码在发出警告,将来代码运行时如果没有及时关注到版本的问题,可能solver的参数会发生改变。所以,最安全的方法并不是通过ignore消除警告,而是指定一个solver参数。

参阅官方文档:

solver : str, {'newton-cg', 'lbfgs', 'liblinear', 'sag', 'saga'}, \default: 'liblinear'.

Algorithm to use in the optimization problem.

- For small datasets, 'liblinear' is a good choice, whereas 'sag' and

'saga' are faster for large ones.

- For multiclass problems, only 'newton-cg', 'sag', 'saga' and 'lbfgs'

handle multinomial loss; 'liblinear' is limited to one-versus-rest

schemes.

- 'newton-cg', 'lbfgs' and 'sag' only handle L2 penalty, whereas

'liblinear' and 'saga' handle L1 penalty.

LogisticRegerssion算法的solver仅支持以下几个参数'liblinear', 'newton-cg', 'lbfgs', 'sag', 'saga'。

解决方法:

传入参数后即可消除警告:model=LogisticRegression(solver=’liblinear’)。

扩展

其他消除警告的方法:

import warnings

warnings.filterwarnings("ignore")

如果觉得内容不错,记得点“好看

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20190221G0Z24W00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券