Scikit recognizes the error message "Accuracy and F-score are not defined and are set to 0.0 in methods",

Im working on a binary classification model, the classifier is naive bays. I have an almost balanced dataset, however I get the following error message when I predict:

UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples.
  'precision', 'predicted', average, warn_for)

I am using gridsearch with CV k-fold 10. The test suite and predictions contain both classes, so I do not understand the messages. I am working on the same dataset, train / test section, cv and random seed for 6 other models, and they work fine. Data comes from outside to the data frame, randomization and seed are captured. Then the naive classification of the fill classes of the file class is at the beginning before this code snippet.

X_train, X_test, y_train, y_test, len_train, len_test = \
     train_test_split(data['X'], data['y'], data['len'], test_size=0.4)
pipeline = Pipeline([
    ('classifier', MultinomialNB()) 
])

cv=StratifiedKFold(len_train, n_folds=10)

len_train = len_train.reshape(-1,1)
len_test = len_test.reshape(-1,1)

params = [
  {'classifier__alpha': [0, 0.0001, 0.001, 0.01]}

]

grid = GridSearchCV(
    pipeline,
    param_grid=params,
    refit=True,  
    n_jobs=-1, 
    scoring='accuracy',
    cv=cv, 
)

nb_fit = grid.fit(len_train, y_train)

preds = nb_fit.predict(len_test)

print(confusion_matrix(y_test, preds, labels=['1','0']))
print(classification_report(y_test, preds))

"" python , , ?

+5
1

aadel, , , TP/(TP + FP) (.. / ). 0, , undefined. F1 , , .

, :

import warnings
import sklearn.exceptions
warnings.filterwarnings("ignore", category=sklearn.exceptions.UndefinedMetricWarning)
+2

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


All Articles