|
|
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 |
|
|
) |
|
|
|
|
|
|
|
|
df = pd.read_csv("edc_sanitized_with_fingerprints.csv") |
|
|
|
|
|
|
|
|
df["fingerprint"] = df["fingerprint"].apply(eval) |
|
|
|
|
|
|
|
|
X = np.array(df["fingerprint"].tolist()) |
|
|
y = df["label"].values |
|
|
|
|
|
|
|
|
X_train, X_test, y_train, y_test = train_test_split( |
|
|
X, y, test_size=0.2, stratify=y, random_state=42 |
|
|
) |
|
|
|
|
|
|
|
|
clf = RandomForestClassifier(random_state=42) |
|
|
clf.fit(X_train, y_train) |
|
|
|
|
|
|
|
|
y_pred = clf.predict(X_test) |
|
|
y_proba = clf.predict_proba(X_test)[:, 1] |
|
|
|
|
|
|
|
|
print(classification_report(y_test, y_pred)) |
|
|
|
|
|
|
|
|
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() |
|
|
|
|
|
|
|
|
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() |
|
|
|
|
|
|