Точность и F-оценка плохо определены и устанавливаются на 0,0 в метках без прогнозируемых выборок. «точность», «прогнозируемый», средний, warn_for) - PullRequest
0 голосов
/ 04 мая 2019

Я пытаюсь построить кривую ROC для задачи классификации нескольких классов, код взят из https://scikit -learn.org / stable / auto_examples / model_selection / plot_roc.html

Я передаю метки и предсказываемые вероятности softmax из нейронной модели. Я получаю следующую ошибку

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)

В чем проблема?

код

def plot_multiclass_ROC(y_test, y_score, n_classes=7):

    y_test = label_binarize(y_test, classes=[0, 1, 2, 3, 4, 5, 6])
    # Import some data to play with
    lw = 2

    # Compute ROC curve and ROC area for each class
    fpr = dict()
    tpr = dict()
    roc_auc = dict()

    # print(y_test.shape, y_score.shape)
    # print(y_test)
    # print(y_score)
    # exit()
    for i in range(n_classes):
        fpr[i], tpr[i], _ = roc_curve(y_test[:, i], y_score[:, i])
        roc_auc[i] = auc(fpr[i], tpr[i])

    # Compute micro-average ROC curve and ROC area
    fpr["micro"], tpr["micro"], _ = roc_curve(y_test.ravel(), y_score.ravel())
    roc_auc["micro"] = auc(fpr["micro"], tpr["micro"])

    # Compute macro-average ROC curve and ROC area

    # First aggregate all false positive rates
    all_fpr = np.unique(np.concatenate([fpr[i] for i in range(n_classes)]))

    # Then interpolate all ROC curves at this points
    mean_tpr = np.zeros_like(all_fpr)
    for i in range(n_classes):
        mean_tpr += interp(all_fpr, fpr[i], tpr[i])

    # Finally average it and compute AUC
    mean_tpr /= n_classes

    fpr["macro"] = all_fpr
    tpr["macro"] = mean_tpr
    roc_auc["macro"] = auc(fpr["macro"], tpr["macro"])

    # Plot all ROC curves
    plt.figure()
    plt.plot(fpr["micro"], tpr["micro"],
             label='micro-average ROC curve (area = {0:0.2f})'
                   ''.format(roc_auc["micro"]),
             color='deeppink', linestyle=':', linewidth=4)

    plt.plot(fpr["macro"], tpr["macro"],
             label='macro-average ROC curve (area = {0:0.2f})'
                   ''.format(roc_auc["macro"]),
             color='navy', linestyle=':', linewidth=4)

    colors = cycle(['aqua', 'darkorange', 'cornflowerblue', "red", "green", "brown", "purple"])
    for i, color in zip(range(n_classes), colors):
        plt.plot(fpr[i], tpr[i], color=color, lw=lw,
                 label='ROC curve of class {0} (area = {1:0.2f})'
                       ''.format(i, roc_auc[i]))

    plt.plot([0, 1], [0, 1], 'k--', lw=lw)
    plt.xlim([0.0, 1.0])
    plt.ylim([0.0, 1.05])
    plt.xlabel('False Positive Rate')
    plt.ylabel('True Positive Rate')
    plt.title('Some extension of Receiver operating characteristic to multi-class')
    plt.legend(loc="lower right")
    plt.show()

1 Ответ

0 голосов
/ 04 мая 2019

Он предупреждает о том, что тестовый набор, который вы проходите для оценки, содержит только 1 метку, поэтому оценка f-1 и точность и отзыв меток, которые не обнаружены в тестовом наборе, будут установлены на 0,0.

...