分类 与预测算法评价

  1. 混淆矩阵

针对二分类问题,将实例分成正类(postive)或者负类(negative)。但是实际中分类时,会出现四种情况.

  • 若一个实例是正类并且被预测为正类,即为真正类(True Postive TP)
  • 若一个实例是正类,但是被预测成为负类,即为假负类(False Negative FN)
  • 若一个实例是负类,但是被预测成为正类,即为假正类(False Postive FP)
  • 若一个实例是负类,但是被预测成为负类,即为真负类(True Negative TN)

由上表可得出横,纵轴的计算公式:

  1. 真正类率(True Postive Rate)TPR: TP/(TP+FN),代表分类器预测的正类中实际正实例占所有正实例的比例。Sensitivity ——- 纵坐标
  2. 负正类率(False Postive Rate)FPR: FP/(FP+TN),代表分类器预测的正类中实际负实例占所有负实例的比例。1-Specificity —— 横坐标
  3. 真负类率(True Negative Rate)TNR: TN/(FP+TN),代表分类器预测的负类中实际负实例占所有负实例的比例,TNR=1-FPR。Specificity
# scikit-learn 计算混淆矩阵
from sklearn.metrics import confusion_matrix
Model.fit(X_train,y_train)
y_pred = Model.predict(X_test)   # Model根据选择的不同模型,写法不同
confmat = confusion_matrix(y_true=y_test, y_pred=y_pred)
print(confmat)
# [[71 1]
# [ 2 40]]
# 绘制混淆矩阵
fig,ax= plt.subplots()
ax.matshow(confmat, cmap=plt.cm.Blues, alpha=0.3)
for i in range(confmat.shape[0]):
    for j in range(confmat.shape[1]):
        ax.text(x=j, y=i, s=confmat[i,j],va='center', ha='center')
plt.xlabel('predicted label')
plt.ylabel('true label')
plt.show()

ROC曲线

接收者操作特征(receiver operating characteristic),roc曲线上每个点反映着对同一信号刺激的感受性

横轴:负正类率特异度
代表分类器预测的正类中实际负实例占所有负实例的比例。1-Specificity

纵轴:真正类率灵敏度 Sensitivity(正类覆盖率)
代表分类器预测的正类中实际正实例占所有正实例的比例。

横轴FPR:1-TNR,1-Specificity,FPR越大,预测正类中实际负类越多。

纵轴TPR:Sensitivity(正类覆盖率),TPR越大,预测正类中实际正类越多。

理想目标:TPR=1,FPR=0,即图中(0,1)点,故ROC曲线越靠拢(0,1)点,越偏离45度对角线越好,Sensitivity、Specificity越大效果越好。

反映分类器 正确分类的统计概率值越接近于1则该算法效果越好

from sklearn.metrics import roc_curve, auc
from scipy import interp  # interp 线性插值
X_train2 = X_train[:, [4,14]]
cv = StratifiedKFold(y_train, n_folds=3, random_state=1)
fig = plt.figure()
mean_tpr=0.0
mean_fpr=np.linspace(0,1,100)
all_tpr = []
# plot 每个fold的ROC曲线,这里fold的数量为3,被StratifiedKFold指定
for i, (train,test) in enumerate(cv):
    #返回预测的每个类别(这里为0或1)的概率  probas[:,0] – 预测为0的概率,probas[:,1]—预测为1的概率
    probas = pipe_lr.fit(X_train2[train],y_train[train]).predict_proba(X_train2[test])
    fpr,tpr,thresholds = roc_curve(y_train[test],probas[:,1],pos_label=1)
    mean_tpr += interp(mean_fpr, fpr,tpr)
    mean_tpr[0]=0.0
    roc_auc=auc(fpr,tpr)
    plt.plot(fpr,tpr,linewidth=1,label='ROC fold %d (area = %0.2f)' % (i+1, roc_auc))
# plot random guessing line
plt.plot([0,1],[0,1],linestyle='--',color=(0.6,0.6,0.6),label='random guessing')
mean_tpr /= len(cv)
mean_tpr[-1] = 1.0
mean_auc = auc(mean_fpr, mean_tpr)
plt.plot(mean_fpr, mean_tpr, 'k--', label='mean ROC (area = %0.2f)' % mean_auc, lw=2)


# plot perfect performance line
plt.plot([0, 0, 1], [0, 1, 1], lw=2, linestyle=':', color='black', label='perfect performance')
# 设置x,y坐标范围
plt.xlim([-0.05,1.05])
plt.ylim([-0.05,1.05])
plt.xlabel('false positive rate')
plt.ylabel('true positive rate')
plt.title('Receiver Operator Charateristic')
plt.legend(loc='lower right')
plt.show()

Accuracy准确率

准确率的定义是对于给定的测试数据集,分类器正确分类的样本数与总样本数之比。公式为:

A(M)=\frac{TN+TP}{TN+FP+FN+TP}

Precision精确率

精确率计算的是: 预测结果中符合实际值的比例,可以理解为没有“误报”的情形,公式为:

A(M)=\frac{TP}{TP+FP}

Recall 召回率

召回率计算的是:正确分类的数量与所有“应该”被正确分类(符合目标标签)的数量的比例,可以理解为召回率对应的没有“漏报”的情形。公式为:

R(M)=\frac{TP}{TP+FN}

F1 score

F1 值是精确率和召回率的调和均值,定义为:

\frac{2}{F_1}+\frac{1}{P}+\frac{1}{R}
准确率和召回率是互相影响的,理想情况下肯定是做到两者都高,但是一般情况下准确率高、召回率就低,召回率低、准确率高,当然如果两者都低,那是什么地方出问题了。当精确率和召回率都高时,F1的值也会高。在两者都要求高的情况下,可以用F1来衡量。

Kappa统计

Kappa统计是比较两个或多个观测人员对同一个事物或同一个事物被观察多次观测结果是否一致,以由于机遇造成的一致性和实际观测的一致性之间的差别作为评价基础的统计指标

Kappa的取值在-1到+1之间

你可能感兴趣的:(分类 与预测算法评价)