UndefinedMetricWarning:Recall 和 F-score 定义错误,在没有真实样本的标签中设置为 0.0。 'recall'、'true'、平均值、warn_for)

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

当我使用下面的代码计算 precision_recall_fscore_support 一个 class(只有 1s)

import numpy as np
from sklearn.metrics import precision_recall_fscore_support

#make arrays
ytrue = np.array(['1', '1', '1', '1', '1','1','1','1'])
ypred = np.array(['0', '0', '0', '1', '1','1','1','1'])

#keep only 1
y_true, y_pred = zip(*[[ytrue[i], ypred[i]] for i in range(len(ytrue)) if ytrue[i]=="1"])

#get scores
precision_recall_fscore_support(y_true, y_pred, average='weighted')

我收到以下警告:

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

并输出:

(1.0, 0.625, 0.76923076923076927, None)

我在 SO 上发现了以下具有类似警告的内容,但我认为它不适用于我的问题。

问题:我的输出结果是否有效,或者我应该关注警告消息吗?如果是这样,我的代码有什么问题以及如何解决?

你好,我找到了这个问题的解决方案,你需要使用:

cv = ShuffleSplit(n_splits=10, test_size=0.3, random_state=0)

我正在使用 knn,这解决了问题

代码:

def knn(self,X_train,X_test,Y_train,Y_test):

   #implementación del algoritmo
   knn = KNeighborsClassifier(n_neighbors=3).fit(X_train,Y_train)
   #10XV
   cv = ShuffleSplit(n_splits=10, test_size=0.3, random_state=0)
   puntajes = sum(cross_val_score(knn, X_test, Y_test, 
                                        cv=cv,scoring='f1_weighted'))/10

   print(puntajes)

**Link:** https://scikit-learn.org/stable/modules/cross_validation.html