[파이썬][scikit-learn] scikit-learn에서 ROC AUC

In machine learning, the Receiver Operating Characteristic (ROC) curve is a common tool for evaluating the performance of binary classification models. The area under the ROC curve (AUC) is a popular metric used to assess the model’s discrimination ability or the effectiveness in distinguishing between the two classes.

Scikit-learn is a widely used machine learning library in Python that provides several functions and tools for model evaluation, including calculating the ROC AUC score.

Calculating ROC AUC using scikit-learn

To calculate the ROC AUC score using scikit-learn, you can follow these steps:

  1. Import the necessary libraries: Begin by importing the required modules from scikit-learn.

    from sklearn.metrics import roc_auc_score
    
  2. Prepare the true labels and predicted probabilities: Assuming you have the true labels and predicted probabilities from your model, gather them into separate arrays.

    true_labels = [...]  # True labels (0 or 1)
    predicted_probs = [...]  # Predicted probabilities for class 1
    
  3. Calculate the ROC AUC score: Use the roc_auc_score function from scikit-learn to compute the ROC AUC score.

    auc_score = roc_auc_score(true_labels, predicted_probs)
    
  4. Output the ROC AUC score: Print or display the calculated ROC AUC score.

    print(f"ROC AUC score: {auc_score}")
    

It is essential to note that the predicted probabilities should be the probabilities of belonging to the positive class (class 1 in binary classification). If your model outputs probabilities for the negative class (class 0), you can simply invert the probabilities by subtracting them from 1.

Conclusion

Evaluating the performance of binary classification models is a crucial step in machine learning. The ROC AUC score provides a comprehensive assessment of a model’s ability to distinguish between classes. Using scikit-learn, you can easily calculate the ROC AUC score with just a few lines of code. This metric can help you compare different models or evaluate the performance of your own model.