Scikit F-score metric error

I am trying to predict a set of labels using Logistic Regression from SciKit. My data is really unbalanced (there are many labels “0” than “1”), so I have to use the F1 metric in the cross-validation step to “balance” the result.

[Input]
X_training, y_training, X_test, y_test = generate_datasets(df_X, df_y, 0.6)
logistic = LogisticRegressionCV(
    Cs=50,
    cv=4,
    penalty='l2', 
    fit_intercept=True,
    scoring='f1'
)
logistic.fit(X_training, y_training)
print('Predicted: %s' % str(logistic.predict(X_test)))
print('F1-score: %f'% f1_score(y_test, logistic.predict(X_test)))
print('Accuracy score: %f'% logistic.score(X_test, y_test))

[Output]
>> Predicted: [0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0]
>> Actual:    [0 0 0 1 0 0 0 0 0 1 1 0 0 1 0 0 0 0 0 0 0 1 1]
>> F1-score: 0.285714
>> Accuracy score: 0.782609
>> C:\Anaconda3\lib\site-packages\sklearn\metrics\classification.py:958:  
   UndefinedMetricWarning:
   F-score is ill-defined and being set to 0.0 due to no predicted samples.

Of course, I know that the problem is related to my data set: it is too small (this is just an example of the real one). However, can anyone explain the meaning of the "UndefinedMetricWarning" warning that I see? What is really going on behind the curtains?

+5
2

, , , , sklearn.

+4

- "UndefinedMetricWarning", ? ?

fooobar.com/questions/189138/...:

https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/metrics/classification.py

F1 = 2 * ( * )/( + )

precision = TP/(TP + FP), , - 0.

= TP/(TP + FN), class - TP - 0 - 0.

, 0/0.

( () ), class_weight="balanced":

logistic = LogisticRegressionCV(
    Cs=50,
    cv=4,
    penalty='l2', 
    fit_intercept=True,
    scoring='f1',
    class_weight="balanced"
)

LogisticRegressionCV :

"" y , n_samples / (n_classes * np.bincount(y)).

0

Source: https://habr.com/ru/post/1016243/


All Articles