roc curve
import numpy as np import matplotlib.pyplot as plt from sklearn.datasets import make_classification from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.linear_model import LogisticRegression from sklearn.metrics import roc_curve, auc, confusion_matrix, classification_report # Generate a synthetic dataset X, y = make_classification(n_samples=1000, n_features=20, n_classes=2, random_state=42) # Split the data into training and testing sets X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42) # Scale the features scaler = StandardScaler() X_train_scaled = scaler.fit_transform(X_train) X_test_scaled = scaler.transform(X_test) # Train the logistic regression classifier lr = LogisticRegression() lr.fit(X_train_scaled, y_train) # Calculate the predicted probabilities y_pred_proba = lr.predict_proba(X_test_scaled)[:, 1] # Compute the ROC curve fpr, tpr, thresholds = roc_curve(y_test, y_pred_proba) roc_auc = auc(fpr, tpr) # Find the optimal threshold optimal_idx = np.argmax(tpr - fpr) optimal_threshold = thresholds[optimal_idx] # Apply the optimal threshold to the predicted probabilities y_pred_optimal = (y_pred_proba >= optimal_threshold).astype(int) # Evaluate the classifier's performance with the optimal threshold conf_matrix = confusion_matrix(y_test, y_pred_optimal) print("Confusion Matrix:nn", conf_matrix) class_report = classification_report(y_test, y_pred_optimal) print("nnClassification Report:nn", class_report)