import pandas as pd import numpy as np import matplotlib.pyplot as plt from sklearn.ensemble import RandomForestClassifier from sklearn.model_selection import train_test_split from sklearn.metrics import ( classification_report, confusion_matrix, ConfusionMatrixDisplay, roc_curve, auc ) # Load the dataset (adjust path if needed) df = pd.read_csv("edc_sanitized_with_fingerprints.csv") # Convert fingerprint column (stored as strings) back into lists df["fingerprint"] = df["fingerprint"].apply(eval) # Features and labels X = np.array(df["fingerprint"].tolist()) y = df["label"].values # Split into training and testing sets (80/20 split with stratification) X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.2, stratify=y, random_state=42 ) # Train the classifier clf = RandomForestClassifier(random_state=42) clf.fit(X_train, y_train) # Predictions and probabilities y_pred = clf.predict(X_test) y_proba = clf.predict_proba(X_test)[:, 1] # Probabilities for class 1 # Print classification report print(classification_report(y_test, y_pred)) # --- Confusion Matrix --- cm = confusion_matrix(y_test, y_pred) disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=clf.classes_) disp.plot(cmap="Blues") plt.title("Confusion Matrix") plt.savefig("confusion_matrix.png") plt.show() # --- ROC Curve --- fpr, tpr, _ = roc_curve(y_test, y_proba) roc_auc = auc(fpr, tpr) plt.figure() plt.plot(fpr, tpr, color="darkorange", lw=2, label=f"ROC curve (AUC = {roc_auc:.2f})") plt.plot([0, 1], [0, 1], color="navy", lw=2, linestyle="--") plt.xlim([0.0, 1.0]) plt.ylim([0.0, 1.05]) plt.xlabel("False Positive Rate") plt.ylabel("True Positive Rate") plt.title("Receiver Operating Characteristic (ROC) Curve") plt.legend(loc="lower right") plt.savefig("roc_curve.png") plt.show()