from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import roc_curve, roc_auc_score
from sklearn.model_selection import train_test_split
import numpy as np
import matplotlib.pyplot as plt
# Load the iris dataset
X, y = load_iris(return_X_y=True)
# Convert multi-class problem to binary classification problem
y_binary = np.where(y == 0, 1, 0)
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y_binary, test_size=0.3, random_state=42)
# Fit the model to the training data
model = LogisticRegression()
model.fit(X_train, y_train)
# Make predictions on the testing data
y_pred_proba = model.predict_proba(X_test)[:,1]
# Calculate the ROC curve and AUC
fpr, tpr, thresholds = roc_curve(y_test, y_pred_proba)
auc = roc_auc_score(y_test, y_pred_proba)
# Plot the ROC curve
plt.plot(fpr, tpr)
plt.title('ROC curve (AUC = %0.2f)' % auc)
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.show()
# Print the AUC
print("AUC:", auc)