📜  sklearn roc 曲线 - Python (1)

📅  最后修改于: 2023-12-03 15:20:09.273000             🧑  作者: Mango

Sklearn ROC Curve

In machine learning, a Receiver Operating Characteristic (ROC) curve is a graphical plot that illustrates the performance of a binary classifier system as its discrimination threshold is varied.

The ROC curve is created by plotting the true positive rate (TPR) against the false positive rate (FPR) at various threshold settings. An ROC curve plots TPR vs. FPR at different classification thresholds.

Sklearn provides a convenient and easy-to-use method for computing and plotting ROC curves. Let's go through the steps to generate and plot an ROC curve using Sklearn.

Step 1: Import Required Libraries

To generate an ROC curve, we need to import roc_curve and auc methods from Sklearn's metrics module. We also need Matplotlib for plotting the ROC curve.

from sklearn.metrics import roc_curve, auc
import matplotlib.pyplot as plt
Step 2: Compute ROC Curve and AUC

Next, we need to compute the ROC curve and area under the curve (AUC) using the roc_curve and auc methods.

# y_true: true values of binary class
# y_score: corresponding score of binary class
fpr, tpr, threshold = roc_curve(y_true, y_score)
roc_auc = auc(fpr, tpr)
Step 3: Plot ROC Curve

The final step is to plot the ROC curve using Matplotlib.

plt.title('ROC Curve')
plt.plot(fpr, tpr, 'b', label = 'AUC = %0.2f' % roc_auc)
plt.legend(loc = 'lower right')
plt.plot([0, 1], [0, 1],'r--')
plt.xlim([-0.1, 1.0])
plt.ylim([-0.1, 1.0])
plt.ylabel('True Positive Rate')
plt.xlabel('False Positive Rate')
plt.show()

The resulting plot will show the ROC curve for the binary classifier system, along with the AUC value.

Conclusion

In this tutorial, we learned how to use Sklearn to generate and plot ROC curves for binary classifier systems. The ROC curve is an important tool for evaluating the performance of machine learning models, and can help in selecting the best model for a given problem.