Eun*_*Lee 5 python suppress-warnings scikit-learn deprecation-warning
我试图用以下方法使DeprecationWarning保持沉默.
import warnings
warnings.filterwarnings(action='ignore')
from sklearn.ensemble import RandomForestRegressor
Run Code Online (Sandbox Code Playgroud)
但是,它仍然显示:
DeprecationWarning:numpy.core.umath_tests是一个内部NumPy模块,不应导入.它将在未来的NumPy版本中删除.来自numpy.core.umath_tests导入inner1d
为什么会发生这种情况,我该如何解决?
我在python 3.6.6,numpy 1.15.0和scikit-learn 0.19.2上运行它,并且添加category=DeprecationWarning没有帮助.
发生这种情况的原因是 Scikit在您导入时会重置您的 DeprecationWarning 过滤器:
# Make sure that DeprecationWarning within this package always gets printed
warnings.filterwarnings('always', category=DeprecationWarning,
module=r'^{0}\.'.format(re.escape(__name__)))
Run Code Online (Sandbox Code Playgroud)
偷偷摸摸!
我发现的唯一解决方法是暂时抑制 stderr:
import os
import sys
sys.stderr = open(os.devnull, "w") # silence stderr
from sklearn.ensemble import RandomForestRegressor
sys.stderr = sys.__stderr__ # unsilence stderr
Run Code Online (Sandbox Code Playgroud)
wheresys.__stderr__指的是系统的实际标准错误(而不是sys.stderr,它只是告诉 Python 将标准错误打印到哪里)。
| 归档时间: |
|
| 查看次数: |
876 次 |
| 最近记录: |