precision_recall_fscore_support 返回的准确率、精确率和召回率值相同

我正在训练一个逻辑回归分类模型,并尝试使用混淆矩阵来比较结果,并计算精确率、召回率和准确率,代码如下所示

# 逻辑回归分类模型
clf_lr = sklearn.linear_model.LogisticRegression(penalty='l2', class_weight='balanced')
logistic_fit=clf_lr.fit(TrainX, np.where(TrainY >= delay_threshold,1,0))
pred = clf_lr.predict(TestX)
# 打印结果
cm_lr = confusion_matrix(np.where(TestY >= delay_threshold,1,0), pred)
print("Confusion matrix")
print(pd.DataFrame(cm_lr))
report_lr = precision_recall_fscore_support(list(np.where(TestY >= delay_threshold,1,0)), list(pred), average='micro')
print ("\nprecision = %0.2f, recall = %0.2f, F1 = %0.2f, accuracy = %0.2f\n" % \
           (report_lr[0], report_lr[1], report_lr[2], accuracy_score(list(np.where(TestY >= delay_threshold,1,0)), list(pred))))
print(pd.DataFrame(cm_lr.astype(np.float64) / cm_lr.sum(axis=1)))
show_confusion_matrix(cm_lr)
#linear_score = cross_validation.cross_val_score(linear_clf, ArrX, ArrY,cv=10)
#print linear_score

预期结果是

Confusion matrix      0     1
0  4303  2906
1  1060  1731
precision = 0.37, recall = 0.62, F1 = 0.47, accuracy = 0.60
          0         1
0  0.596893  1.041204
1  0.147038  0.620208

然而,我的输出是

Confusion matrix      0     1
0  4234  2891
1  1097  1778
precision = 0.60, recall = 0.60, F1 = 0.60, accuracy = 0.60
          0         1
0  0.594246  1.005565
1  0.153965  0.618435

如何获得正确的结果?


回答:

在像您这样的“二元”情况下(2个类别),您需要使用 average=’binary’ 而不是 average=’micro’。

例如:

TestY = [0, 1, 1, 0, 1, 1, 1, 0, 0, 0]
pred = [0, 1, 1, 0, 0, 1, 0, 1, 0, 0]
# 打印结果
cm_lr = metrics.confusion_matrix(TestY, pred)
print("Confusion matrix")
print(pd.DataFrame(cm_lr))
report_lr = metrics.precision_recall_fscore_support(TestY, pred, average='binary')
print ("\nprecision = %0.2f, recall = %0.2f, F1 = %0.2f, accuracy = %0.2f\n" % \
           (report_lr[0], report_lr[1], report_lr[2], metrics.accuracy_score(TestY, pred)))

输出如下:

Confusion matrix   0  1
0  4  1
1  2  3
precision = 0.75, recall = 0.60, F1 = 0.67, accuracy = 0.70

二元分类有一个默认定义,哪个类别是正类(标记为1的类别)。您可以在此链接中阅读所有平均选项之间的差异。

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注