【问题标题】:How to show Precession, Recall and F1-Score?如何显示 Precession、Recall 和 F1-Score?
【发布时间】:2021-03-01 10:58:44
【问题描述】:

我目前正在显示精度、召回率和 fscore。现在我的问题是我该怎么做?我尝试的是以下内容:

num_users, num_items = train_mat.shape
user_input, item_input, labels = get_train_samples(train_mat, num_negatives)
val_user_input, val_item_input, val_labels = get_train_samples(val_mat, num_negatives)
.
.
.
history = model.fit([np.array(user_input), np.array(item_input)], np.array(labels), 
                 epochs=EPOCHS, verbose=VERBOSE, shuffle=True, batch_size = BATCH_SIZE,
                 validation_data=([np.array(val_user_input), np.array(val_item_input)], np.array(val_labels)),
                  callbacks=CALLBACKS)
.
.
.
# Precision, recall and fscore
from sklearn.metrics import precision_recall_fscore_support, confusion_matrix, roc_curve, auc
precision, recall, fscore, _ = precision_recall_fscore_support(y_test, y_pred, average='weighted')

print('Precision, recall, and F1 score, averaged and weighted by number of instances in each class:')
print('precision: {}'.format(precision))
print('recall: {}'.format(recall))
print('f1 score: {}\n'.format(fscore))

precision, recall, fscore, _ = precision_recall_fscore_support(y_test, y_pred)

print('Precision, recall, and F1 score, per class [0 1]:')
print('precision: {}'.format(precision))
print('recall: {}'.format(recall))
print('f1 score: {}'.format(fscore))


cm = confusion_matrix(y_test, y_pred)
sns.heatmap(cm, annot=True)

很遗憾,我不知道如何获取y_testy_pred。我如何获得这些值?

【问题讨论】:

  • 您从数据集中获得 X_test, y_test。你应该从你的数据集中保留一些数据来测试你训练好的模型。因此,在训练之后,您可以在模型上预测 X_test 并获得 y_pred。最后,将 y_test(即真值)与 y_pred(模型的预测)进行匹配。
  • 谢谢,这意味着我必须从一开始就删除数据(即测试数据)。之后我也做test_user_input, test_item_input, test_labels = get_train_samples (test_mat, num_negatives) 并用这些值调用model.predic
  • 根据您的数据集结构,我认为是这样。您只需将数据集拆分为两个子集,通常称为traintest 数据。
  • 我是否必须再次将测试数据输入到模型中? :)
  • 您只会在训练期结束时使用它来衡量模型的不同分数。例如,您可以查看notebook。请注意我如何在块In [15] 中拆分数据集,然后再次使用块In [27] 中的test 数据来生成混淆矩阵。谢谢。

标签: python metrics precision-recall


【解决方案1】:

你应该有 y_test 作为测试集来测试你的模型,如果你没有这样的集,你可以使用 sklearn train test split 来获取训练集和测试集。这是如何使用它的链接: sklearn traiin test split

当您拥有测试集时,您将执行此操作以获得y_pred

y_pred = model.predict(y_test)

【讨论】:

    猜你喜欢
    • 2016-05-23
    • 1970-01-01
    • 2023-02-08
    • 2021-11-24
    • 2021-04-11
    • 1970-01-01
    • 2019-06-01
    • 2017-12-30
    • 2016-10-03
    相关资源
    最近更新 更多