Solving Python Scikit-learn UndefinedMetricWarning: F-score is ill-defined and being set to 0.0 in labels with no predicted samples

Scikit-learn UndefinedMetricWarning: F-score is ill-defined and being set to 0.0 in labels with no predicted samples is an error which occurs in Python and Scikit-learn when the sample size is small.

In this article I am going to show you why this error is happening and how you can solve this error. I am also going to share with you other possible solutions which have worked for many developers who suffered from this error.

Explaining the Error : Scikit-learn UndefinedMetricWarning: F-score is ill-defined and being set to 0.0 in labels with no predicted samples

First of all, let us check out the error message.

Please make sure that the error you have matches the error bellow. To avoid all kinds of confusion.

                                                                       #
classification.py:1113: UndefinedMetricWarning: F-score is ill-defined and being set to 0.0 in labels with no predicted samples.
'precision', 'predicted', average, warn_for)`
                                                                       #

Bellow, I will present multiple solutions some have worked for me and others have worked for other developers.

Solution 1 : Increase the train data size.

First, If you did split down your data set to small groups. You should probably try to make the groups bigger. Sometimes a very small sample can cause the warning.

You can replace the code bellow.

                                                                       #
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=0)
                                                                       #

With this code.

                                                                       #
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
                                                                       #

I hope the solution above has worked for you. If it did not try the solution bellow.

Solution 2 : use precision_recall_fscore_support

The second solution is to use precision_recall_fscore_support in order to control the warnings and to be more precise “mute the warnings”.

Bellow is an example of how you can use use precision_recall_fscore_support.

                                                                       #
(_, _, f1, _) = metrics.precision_recall_fscore_support(y_test, y_pred, average='weighted', warn_for=tuple())
                                                                       #

Please try the solution bellow if this one did not solve your problem.

Solution 3 : reset the data index

If the solutions above did not work, try using the solution bellow.

When using train_test_split() to split the data, you should make sure to reset the data index.

You should use one of the following lines of code.

                                                                       #
np.array(y_test) for y_true in scores
y_test.reset_index(drop=True)
                                                                       #

This is a good way to solve the issue, I hope this fix has worked for you.

I hope the solutions above fixed your problem, good luck with the scripts to come.

Summing-up

Here we are at the end of the road, at the end of this article, if you solved this error congrats, this was a confusing error for me the first time I encountered it. Make sure to keep coding and keep learning, Python is my favourite programming language, it just needs some patience, cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/