Reputation: 1220
I have really a lot of UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 due to no predicted samples.
resulting from running a randomized search pipeline with cross-validation. I know what is causing this behavior and suggested setting to 0.0 score is currently fine with me, so I want to just silence this warning for now.
I tried:
warnings.filterwarnings('ignore')
and
from sklearn.exceptions import UndefinedMetricWarning
warnings.filterwarnings('ignore', category=UndefinedMetricWarning)
But I am still getting these warnings, even though other answers on StackOverflow suggested that they should be suppressed by these lines (and actually it worked for me some time ago in a notebook).
The warnings.filterwarnings(...)
line is located directly under import
statements, and the warnings are from in one of the nested functions.
Upvotes: 2
Views: 2535
Reputation: 31
For me, the solution in the question works:
from sklearn.exceptions import UndefinedMetricWarning
import warnings
warnings.filterwarnings("ignore", category=UndefinedMetricWarning)
also see: How to disable Python warnings?
I am using this:
Python 3.8.10 (default, Nov 22 2023, 10:22:35)
>>> print('The scikit-learn version is {}.'.format(sklearn.__version__))
The scikit-learn version is 1.0.1.
Upvotes: 0
Reputation: 33147
Use the following:
from sklearn.exceptions import UndefinedMetricWarning
def warn(*args, **kwargs):
pass
import warnings
warnings.warn = warn
# more code here...
# more code here...
Upvotes: 0