19
votes

When I'm fitting sklearn's LogisticRegression using a 1 column python pandas DataFrame (not a Series object), I get this warning:

/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?

3
You can filter the warnings using the warning module: docs.python.org/2/library/warnings.htmlAndreas Mueller

3 Answers

34
votes

You can use this:

import warnings
from sklearn.exceptions import DataConversionWarning
warnings.filterwarnings(action='ignore', category=DataConversionWarning)
16
votes

As posted here,

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

Thanks to Andreas above for posting the link.

3
votes

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, ).

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.