机器学习之ROC曲线以及混淆矩阵

1.ROC曲线

占坑。

-------------------------------------------   分割线  -------------------------------------------

2.混淆矩阵

混淆矩阵的作用对机器学习的学习效果进行评估的一种指标。confusion matrix is to evaluate the accuracy of a classification.它的作用是评估分类的准确度。

混淆矩阵的定义:对于混淆矩阵C,C(i,j) 表示真实分类是第i类,但是预测值为第j类的观测总数。

对于二分类问题(假设1是正类):

C(0,0)表示真反类  TN(true negative)C(0,1)表示假正类  FP(false negative)
C(1,0)表示假反类  FP(false negative)C(1,1)表示真正类  TP(true positive)

混淆矩阵的实现:

    通过使用scikit-learn模块计算混淆矩阵,

  sklearn.metrics.confusion_matrix(y_true, y_pred, labels=None, sample_weight=None).

    下面对参数进行说明:(注:表格中的数组类型在python中对应为列表list()类型)  



Parameters:

参数:

y_true : 长度为n_samples的数组类型

              表示真实分类。Ground truth (correct) target values.

y_pred : 长度为n_samples的数组类型

               表示由分类器对n个样例的预测分类值。Estimated targets as returned by a classifier.

labels :   [可选参数],长度为n_classes的数组类型 (n_classes表示类别/标签个数)

               这个参数是标签的列表形式,影响输出的混淆矩阵C的行列表示的含义,先输出哪一类的值

                 该矩阵可能被用于重排或者选择其中一个子集计算混淆矩阵。 

              如果不给这个参数(即为None),那么 y_true or y_pred中至少出现一次的值将被用于排列顺序

                 注:1. 混淆矩阵的列表示类别的顺序   2. y_true or y_pred中出现的值就表示类标签

               List of labels to index the matrix. This may be used to reorder or select a subset of labels. 

               If none is given, those that appear at least once in y_true or y_pred are used in sorted order.

sample_weight : 类似长度为n_samples的数组形式,可选参数。

                             样例的权重向量。Sample weights.

Returns:

返回值:

C : array, shape = [n_classes, n_classes]


返回混淆矩阵(Confusion matrix)C,shape=(n_classes, n_classes)。

混淆矩阵的性质:

        矩阵的对角线表示正确分类的个数,非对角线的点是分类错误的情况。


混淆矩阵的实例:

1.计算混淆矩阵:

from sklearn.metrics import confusion_matrix
y_true = [2, 0, 2, 2, 0, 1]
y_pred = [0, 0, 2, 2, 0, 2]
confusion_matrix(y_true, y_pred)

2.使用matplotlib.pyplot画出混淆矩阵:

def plot_confusion_matrix(cm, classes,
                          normalize=False,
                          title='Confusion matrix',
                          cmap=plt.cm.Blues):
    """
    This function prints and plots the confusion matrix.
    Normalization can be applied by setting `normalize=True`.
    """
    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
        print("Normalized confusion matrix")
    else:
        print('Confusion matrix, without normalization')

    print(cm)

    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=45)
    plt.yticks(tick_marks, classes)

    fmt = '.2f' if normalize else 'd'
    thresh = cm.max() / 2.
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j, i, format(cm[i, j], fmt),
                 horizontalalignment="center",
                 color="white" if cm[i, j] > thresh else "black")

    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')


plt.figure()
plot_confusion_matrix(cnf_matrix, classes=class_names,
                      title='Confusion matrix, without normalization')


# Plot normalized confusion matrix
plt.figure()
plot_confusion_matrix(cnf_matrix, classes=class_names, normalize=True,
                      title='Normalized confusion matrix')
plt.show()


-----end-----



  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
交叉验证(Cross Validation)是一种常用的模型评估方法,用于评估机器学习模型的性能。它通过将数据集划分为训练集和验证集,多次训练和验证模型,从而得到模型的平均性能指标。 交叉验证的步骤如下: 1. 将数据集划分为K个大小相等的子集,通常称为折(fold)。 2. 对于每个折,将其作为验证集,其余的折作为训练集。 3. 在每个训练集上训练模型,并在对应的验证集上进行评估。 4. 计算K次验证结果的平均值作为模型的性能指标。 交叉验证可以更准确地评估模型的性能,避免了单次划分数据集可能导致的偶然性结果。常见的交叉验证方法有K折交叉验证、留一交叉验证等。 混淆矩阵(Confusion Matrix)是一种用于衡量分类模型性能的矩阵。它以实际类别和预测类别为基础,将样本分为真正例(True Positive, TP)、真负例(True Negative, TN)、假正例(False Positive, FP)和假负例(False Negative, FN)四种情况。 混淆矩阵的形式如下: 预测为正例 预测为负例 实际为正例 TP FN 实际为负例 FP TN 混淆矩阵可以用于计算多个评估指标,如准确率(Accuracy)、精确率(Precision)、召回率(Recall)和F1值等,从而更全面地评估模型的分类性能。 ROC曲线(Receiver Operating Characteristic Curve)是一种用于评估二分类模型性能的曲线。它以真正例率(True Positive Rate, TPR)为纵轴,假正例率(False Positive Rate, FPR)为横轴,绘制出模型在不同阈值下的性能。 ROC曲线的横轴表示模型的假正例率,纵轴表示模型的真正例率。曲线越靠近左上角,说明模型的性能越好。ROC曲线下的面积(Area Under Curve, AUC)可以用来衡量模型的整体性能,AUC值越大,模型性能越好。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值