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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:04:01  来源:igfitidea点击:

sklearn: Turning off warnings

pythonpandaswarningsscikit-learn

提问by hlin117

When I'm fitting sklearn's LogisticRegressionusing a 1 column python pandasDataFrame(not a Seriesobject), I get this warning:

当我安装sklearnLogisticRegression使用1列蟒pandasDataFrame(不是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

As posted here,

正如这里发布的,

with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    # Do stuff here

Thanks to Andreas above for posting the link.

感谢上面的 Andreas 发布链接。

回答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)