ROC and AUC calculation, sklearn.metrics roc_curve, precision_recall_curve, auc, roc_auc_score, multi-label classification ROC curve drawing

For the calculation of ROC curve and AUC, please refer to the following:

Fully understand and master AUC - Zhihu (zhihu.com)

ROC is insensitive to class imbalance. The defect of the AUC index is that it only reflects the sorting ability of the model, but cannot reflect the goodness of fit of the model. ( The defect of AUC - Zhihu (zhihu.com) )

一、sklearn.metrics.roc_curve(y_truey_score*pos_label=Nonesample_weight=Nonedrop_intermediate=True)

Can only be used for binary classification

The shape of y_true and y_score is (n_samples,); return fpr, tpr and thresholds, and the shape is (n_samples,)

example:

>>> import numpy as np
>>> from sklearn import metrics
>>> y = np.array([1, 1, 2, 2])
>>> scores = np.array([0.1, 0.4, 0.35, 0.8])
>>> fpr, tpr, thresholds = metrics.roc_curve(y, scores, pos_label=2)
>>> fpr
array([0. , 0. , 0.5, 0.5, 1. ])
>>> tpr
array([0. , 0.5, 0.5, 1. , 1. ])
>>> thresholds
array([1.8 , 0.8 , 0.4 , 0.35, 0.1 ])

二、sklearn.metrics.precision_recall_curve(y_trueprobas_pred*pos_label=Nonesample_weight=None)

Similar to sklearn.metrics.roc_curve

三、sklearn.metrics.auc(xy)

This is a general function, given points on a curve,compute Area Under the Curve (AUC) using the trapezoidal rule.

Parameter Description:

example:

>>> import numpy as np
>>> from sklearn import metrics
>>> y = np.array([1, 1, 2, 2])
>>> pred = np.array([0.1, 0.4, 0.35, 0.8])
>>> fpr, tpr, thresholds = metrics.roc_curve(y, pred, pos_label=2)
>>> metrics.auc(fpr, tpr)
0.75

 四、sklearn.metrics.roc_auc_score(y_truey_score*average='macro'sample_weight=Nonemax_fpr=Nonemulti_class='raise'labels=None)

Can be used for binary classification, multi-class single-label and multi-label classification , but some parameters have limited values

The shape of y_true and y_score is (n_samples,) or (n_samples, n_classes)

average parameter: only valid under multi-category. If  None, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data. 'macro' is to calculate the auc of each class, and then take the average; 'micro' is to set the shape to (n_samples, n_classes) Each element in y_true and y_score is regarded as a 0/1gt label and the corresponding probability, and the global auc (Calculate metrics globally by considering each element of the label indicator matrix as a label) is calculated.

Five, python realizes the ROC curve tutorial of binary classification and multi-classification

Python implements ROC curve tutorial for binary classification and multi-classification - Tencent Cloud Developer Community - Tencent Cloud (tencent.com)

reference: 

sklearn.metrics.roc_curve — scikit-learn 1.1.1 documentation

sklearn.metrics.auc — scikit-learn 1.1.1 documentation

sklearn.metrics.roc_auc_score — scikit-learn 1.1.1 documentation

Guess you like

Origin blog.csdn.net/qq_41021141/article/details/126023548