machine-learning - precision_recall_fscore_support 返回相同的准确度、精确度和召回率值

标签 machine-learning scikit-learn data-mining confusion-matrix sklearn-pandas

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

# logistic regression classification model
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)

# print results
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]
# print results
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

Binary 有一个默认定义,即哪个类是正类(带有 1 标签的类)。 您可以在这个link中阅读所有平均选项之间的差异。 .

关于machine-learning - precision_recall_fscore_support 返回相同的准确度、精确度和召回率值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43001014/

相关文章:

matlab - 如何在 Matlab 中计算内核?

machine-learning - Torch - 神经网络的大输入和输出

python - 机器学习模型如何更新?

python - 为文本挖掘映射关系挖掘维基百科

java - 是否有一个设计良好、维护良好的 Java 决策树学习库?

python - 初始化 HiddenMarkovModelTrainer 对象

apache-spark - 为什么 Spark 的 OneHotEncoder 默认删除最后一个类别?

scikit-learn - 在 scikit-learn 中使用 OneHotEncoder 为分类准备序数和标称特征

python - `rcond` 参数将更改为机器精度乘以默认值 `` max(M, N) `` 其中 M 和 N 是输入矩阵维度

web-services - 面向服务的架构和松耦合与 SQL JOINS