Python sklearn:关闭警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29086398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
sklearn: Turning off warnings
提问by hlin117
When I'm fitting sklearn
's LogisticRegression
using a 1 column python pandas
DataFrame
(not a Series
object), I get this warning:
当我安装sklearn
的LogisticRegression
使用1列蟒pandas
DataFrame
(不是Series
对象),我得到这样的警告:
/Library/Python/2.7/site-packages/sklearn/preprocessing/label.py:125:
DataConversionWarning: A column-vector y was passed when a 1d array was
expected. Please change the shape of y to (n_samples, ), for example using
ravel().
y = column_or_1d(y, warn=True)
I know I could easily advert this warning in my code, but how can I turn off these warnings?
我知道我可以轻松地在代码中发布此警告,但如何关闭这些警告?
采纳答案by hlin117
回答by Salvador Dali
Actually the warning tells you exactly what is the problem:
实际上,警告会确切地告诉您问题是什么:
You pass a 2d array which happened to be in the form (X, 1)
, but the method expects a 1d array and has to be in the form (X, )
.
您传递了一个 2d 数组,该数组碰巧在 form 中(X, 1)
,但该方法需要一个 1d 数组并且必须在 form 中(X, )
。
Moreover the warning tells you what to do to transform to the form you need: y.ravel()
. So instead of suppressing a warning it is better to get rid of it.
此外,警告会告诉您如何转换为您需要的形式:y.ravel()
. 因此,与其抑制警告,不如摆脱它。
回答by Jeffrey Zhou
You can use this:
你可以使用这个:
import warnings
from sklearn.exceptions import DataConversionWarning
warnings.filterwarnings(action='ignore', category=DataConversionWarning)