2
votes

I am using F1_score metrics in sklearn. For some training data sets, the total number of y=1(rare case) sets is zero, the F1_score is zero,which is normal. But the sklearn gives the following warning:

"UndefinedMetricWarning: F-score is ill-defined and being set to 0.0 due to no predicted samples".

Does anyone know how to silence this warning? and in general could we silence all kinds of warnings in sklearn ?

1
The accepted answer here seems to have the information you are interested in: stackoverflow.com/questions/43162506/… - Jason Baumgartner
I checked all, but not able to find the one I wanted - saunter

1 Answers

11
votes

You can easily ignore the warnings with the help of warnings module in Python like this.

import warnings
warnings.filterwarnings('ignore') 

For example,

from sklearn.metrics import f1_score

yhat = [0] * 100
y = [0] * 90 + [1] * 10

print(f1_score(y, yhat))

This will throw warning. To avoid that use,

from sklearn.metrics import f1_score

import warnings
warnings.filterwarnings('ignore') 

yhat = [0] * 100
y = [0] * 90 + [1] * 10

print(f1_score(y, yhat))

This wont show the warning.