Halcom 发表于 2017-6-10 23:02:49

混淆矩阵confusion_mat计算预测正确率和召回率

混淆矩阵confusion_mat计算预测正确率和回召率
正样本 负样本
预测为正样本 真阳性 假阳性
预测为负样本 假阴性真阴性

样本预测准确性Accuracy:(真阳性+真阴性)/(真阳性+假阳性+假阴性+真阴性 )
正样本预测精度Precision:真阳性/( 真阳性+假阳性 )
负样本预测精度Precision:真阴性 /( 真阴性+假阴性 )
正样本召回率Recall:真阳性/( 真阳性+假阴性 )
负样本召回率Recall:真阴性 /( 真阴性+假阳性 )
F1指标:2/(1/Precision + 1/Recall)

使用环境:Win7-32bit-matlab2014a
function = confusion_mat( y_actual,y_predicted )
tp=0;tn=0;fp=0;fn=0;
for i=1:length(y_actual)
if y_actual(i)>0
    if y_actual(i)==y_predicted(i)
      tp=tp+1;
    else
      fn=fn+1;
    end
end
if y_actual(i)<0
    if y_actual(i)==y_predicted(i)
      tn=tn+1;
    else
      fp=fp+1;
    end
end
end
cm=;
acc=(tp+tn)/(tp+fn+fp+tn);
sens=tp/(tp+fn);
prec=tp/(tp+fp);
fm=(2*prec*sens)/(prec+sens);
end等价于python:# 从sklearn.metrics里导入classification_report模块。
from sklearn.metrics import classification_report
# 使用逻辑斯蒂回归模型自带的评分函数score获得模型在测试集上的准确性结果。
print 'Accuracy of LR Classifier:', lr.score(X_test, y_test)
# 利用classification_report模块获得LogisticRegression其他三个指标的结果。
print classification_report(y_test, lr_y_predict, target_names=['0', '1'])





页: [1]
查看完整版本: 混淆矩阵confusion_mat计算预测正确率和召回率