Python如何过滤特定警告? [英] Python how to filter specific warning?

查看:64
本文介绍了Python如何过滤特定警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何过滤python中特定模块的特定警告?

How to filter the specific warning for specific module in python?

ERROR:

cross_val_score(model, df_Xtrain,ytrain,cv=2,scoring='r2')

/usr/local/lib/python3.6/dist-packages/sklearn/linear_model/_ridge.py:148: LinAlgWarning: Ill-conditioned matrix (rcond=3.275e-20): result may not be accurate.
  overwrite_a=True).T

我的尝试

import warnings
warnings.filterwarnings(action='ignore',module='sklearn')

cross_val_score(model, df_Xtrain,ytrain,cv=2,scoring='r2')

This works but I assume it ignores all the warnings such as deprecation warnings. I would like to exclude only LinAlgWarning

必填赞

import warnings
warnings.filterwarnings(action='ignore', category='LinAlgWarning', module='sklearn')

有没有类似这种过滤器特定警告的方法?

Is there a way like this filter specific warning?

推荐答案

您必须将 category 作为 WarningClass not in String 传递:

You have to pass category as WarningClass not in String:

from scipy.linalg import LinAlgWarning

warnings.filterwarnings(action='ignore', category=LinAlgWarning, module='sklearn')

这篇关于Python如何过滤特定警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆