UndefinedMetricWarning:Recall和F-score定义不明确,在没有真实样本的标签中设置为0.0。 '召回',' true',average,warn_for)

时间:2018-02-26 01:38:12

标签: python machine-learning scikit-learn precision-recall

当我使用以下代码计算一类的precision_recall_fscore_support时(只有1 s)

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 UndefinedMetricWarning: F-score is ill-defined and being set to 0.0 in labels with no predicted samples上发现了以下类似警告,但我认为它不适用于我的问题。

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

1 个答案:

答案 0 :(得分:0)

您好,我找到了解决此问题的方法,您需要使用:

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)

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