sklearn: Turning off warnings

34,608

Solution 1

As posted here,

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

Thanks to Andreas above for posting the link.

Solution 2

You can use this:

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

Solution 3

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.

Solution 4

Note: In case you want to ignore or get rid of such warning

import warnings 
warnings.filterwarnings("ignore")

Otherwise if you're looking into the cause of the issue this might be helpful.

When you try to fit your model, make sure X_test and y_test are similar to those used in training data. in other words X_train and X_test should be the same with the same features and same for X_test and y_test

For example: np.array(X_test) is not same as X_test, given that X_train is just a numpy's DataFrame and X_test was splitted out from dataset:

# imports 
...
clf = RandomForestClassifier(n_estimators=100)
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2)    
# the following produce warning since X_test's shape is different than X_train
y_predicts  = clf.predict(np.array(X_test))
# no warning (both are same) 
y_predicts  = clf.predict(X_test)
Share:
34,608

Related videos on Youtube

hlin117
Author by

hlin117

Software Engineer at Uber Technologies. Previously student at the University of Illinois at Urbana-Champaign. Interests include Machine Learning Bioinformatics Mathematics

Updated on March 08, 2022

Comments

  • hlin117
    hlin117 about 2 years

    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?

  • pirho
    pirho over 6 years
    While this may answer the question, it is better to explain the essential parts of the answer and possibly what was the problem with OPs code.
  • Roland Pihlakas
    Roland Pihlakas over 6 years
    @pirho OP probably already knows about the cause of the problem and explicitly mentions the desire to turn off the warning only instead of solving the "problem".
  • gented
    gented over 5 years
    It is worth mentioning that one must import warnings as well.
  • Tomas G.
    Tomas G. over 4 years
    import warnings
  • develarist
    develarist over 4 years
    how to disable Convergence Warnings, for example, for estimators that use sklearn's coordinate descent algorithm? Replacing DataConversionWarning with ConvergenceWarning in the above doesn't work.
  • Tobias
    Tobias about 2 years
    Note that this answer does also work within joblib threads. Other answers, i.e., setting a warning filter globally, does not work when joblib uses the loky backend which spawns separate processes for each task.