sklearn: Turning off warnings
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)
Related videos on Youtube
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, 2022Comments
-
hlin117 about 2 years
When I'm fitting
sklearn
'sLogisticRegression
using a 1 column pythonpandas
DataFrame
(not aSeries
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?
-
Andreas Mueller about 9 yearsYou can filter the warnings using the warning module: docs.python.org/2/library/warnings.html
-
-
pirho over 6 yearsWhile 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 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 over 5 yearsIt is worth mentioning that one must
import warnings
as well. -
Tomas G. over 4 yearsimport warnings
-
develarist over 4 yearshow 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 about 2 yearsNote that this answer does also work within
joblib
threads. Other answers, i.e., setting a warning filter globally, does not work whenjoblib
uses theloky
backend which spawns separate processes for each task.