如何过滤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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
import warnings
warnings.filterwarnings(action='ignore', category='LinAlgWarning', module='sklearn')
Run Code Online (Sandbox Code Playgroud)
有没有像这样过滤特定警告的方法?
我有一个像这样开始的pythonscript:
#!/usr/bin/env python
import matplotlib
matplotlib.use("Agg")
from matplotlib.dates import strpdate2num
import numpy as np
import pylab as pl
from cmath import rect, phase
Run Code Online (Sandbox Code Playgroud)
它就像一个魅力,但我的编辑抱怨道:E402 module level import not at top of file [pep8].
如果我matplotlib.use("Agg")向下移动,脚本将无法正常工作.
我应该忽略错误吗?或者有办法解决这个问题吗?
编辑:我知道PEP8说这只是一个建议,它可能会被忽略,但我希望有一个很好的方法来初始化模块而不破坏PEP8指南,因为我不认为我可以让我编辑器在每个文件的基础上忽略此规则.
EDIT2:我正在使用Atom和linter-pylama
I'm using vscode with the python plugin and autopep8 with
"editor.formatOnSave": true.
I have local packages I need to import, so I have something like
import sys
sys.path.insert(0, '/path/to/packages')
import localpackage
Run Code Online (Sandbox Code Playgroud)
but when I save, vscode/autopep8 moves all import statements before code, so python can't find my local package.
import sys
import localpackage
sys.path.insert(0, '/path/to/packages')
Run Code Online (Sandbox Code Playgroud)
how can I tell vscode/autopep8 that it's okay to put a statement before imports, or is there a more correct way of importing local …