Auc Roc Curve in Python
The AUC-ROC Curve (Area Under the Receiver Operating Characteristic Curve) is used to evaluate the performance of a binary classification model. It measures the ability of the classifier to distinguish between classes.
? Steps to Plot AUC-ROC Curve in Python
We will use Scikit-learn to compute the ROC curve and AUC score.
1?? Import Required Libraries
import numpy as npimport matplotlib.pyplot as pltfrom sklearn.metrics import roc_curve, aucfrom sklearn.model_selection import train_test_splitfrom sklearn.ensemble import RandomForestClassifierfrom sklearn.datasets import make_classification2?? Generate Synthetic Data
# Create a synthetic datasetX, y = make_classification(n_samples=1000, n_features=10, random_state=42)# Split into training and test setsX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)3?? Train a Classification Model
# Train a Random Forest Classifiermodel = RandomForestClassifier()model.fit(X_train, y_train)# Get predicted probabilities for the positive classy_scores = model.predict_proba(X_test)[:, 1]4?? Compute ROC Curve and AUC Score
# Compute ROC curvefpr, tpr, _ = roc_curve(y_test, y_scores)# Compute AUC scoreroc_auc = auc(fpr, tpr)# Print AUC Scoreprint("AUC Score:", roc_auc)5?? Plot ROC Curve
# Plot ROC curveplt.figure(figsize=(8, 6))plt.plot(fpr, tpr, color='blue', label=f'ROC Curve (AUC = {roc_auc:.2f})')plt.plot([0, 1], [0, 1], color='gray', linestyle='--') # Diagonal lineplt.xlabel('False Positive Rate')plt.ylabel('True Positive Rate')plt.title('ROC Curve')plt.legend(loc='lower right')plt.show()? Interpreting AUC Score
AUC = 1.0 ? Perfect classifier ?
AUC > 0.9 ? Excellent model ?
AUC > 0.8 ? Good model ?
AUC > 0.7 ? Fair model ??
AUC ? 0.5 ? Random guessing ?
Let me know if you need any modifications! ?