gpt4 book ai didi

Python如何过滤特定警告?

转载 作者:行者123 更新时间:2023-12-04 13:12:16 28 4
gpt4 key购买 nike

如何过滤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')
有没有像这种过滤器特定警告的方法?

最佳答案

您必须将类别作为 WarningClass 传递,而不是在字符串中:from scipy.linalg import LinAlgWarningwarnings.filterwarnings(action='ignore', category=LinAlgWarning, module='sklearn')

关于Python如何过滤特定警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63979540/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com