sklearn: Turning off warnings
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.
You can use this:
import warnings
from sklearn.exceptions import DataConversionWarning
warnings.filterwarnings(action='ignore', category=DataConversionWarning)
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)
As posted here,
with warnings.catch_warnings():
warnings.simplefilter("ignore")
# Do stuff here
Thanks to Andreas above for posting the link.