Spaces:
Sleeping
Sleeping
Download plot_metrics.py from VSaiTejaReddy004/fraud_detection: direct link, hf CLI and curl.
- Browser
- Download file 2.41 kB
-
https://huggingface.co/spaces/VSaiTejaReddy004/fraud_detection/resolve/main/plot_metrics.py
- Command line
-
hf download hf://spaces/VSaiTejaReddy004/fraud_detection/plot_metrics.py
-
curl -L -o plot_metrics.py https://huggingface.co/spaces/VSaiTejaReddy004/fraud_detection/resolve/main/plot_metrics.py
2.41 kB
| import pandas as pd | |
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay, roc_curve, auc | |
| import joblib | |
| import os | |
| CSV_PATH = "creditcard.csv" | |
| MODEL_PATH = "model.pkl" | |
| def generate_graphs(): | |
| if not os.path.exists(MODEL_PATH) or not os.path.exists(CSV_PATH): | |
| print(f"Missing {MODEL_PATH} or {CSV_PATH}. Please run train_model.py first.") | |
| return | |
| print("Loading Machine Learning model...") | |
| clf = joblib.load(MODEL_PATH) | |
| df = pd.read_csv(CSV_PATH) | |
| from sklearn.model_selection import train_test_split | |
| X = df.drop(columns=["Class", "Time"]) | |
| y = df["Class"] | |
| _, X_test, _, y_test = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y) | |
| y_pred = clf.predict(X_test) | |
| y_proba = clf.predict_proba(X_test)[:, 1] | |
| importances = clf.feature_importances_ | |
| features = X.columns | |
| idx = np.argsort(importances)[::-1] | |
| sorted_features = [features[i] for i in idx] | |
| sorted_importances = [importances[i] for i in idx] | |
| cm = confusion_matrix(y_test, y_pred) | |
| fpr, tpr, _ = roc_curve(y_test, y_proba) | |
| roc_auc = auc(fpr, tpr) | |
| fig, axes = plt.subplots(1, 3, figsize=(18, 6)) | |
| # 1. Feature Importance | |
| axes[0].barh(sorted_features[:15][::-1], sorted_importances[:15][::-1], color='#4C72B0') | |
| axes[0].set_title("Top 15 Feature Importances (What it looks at)") | |
| axes[0].set_xlabel("Importance Score") | |
| # 2. Confusion Matrix | |
| disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=["Normal", "Fraud"]) | |
| disp.plot(ax=axes[1], cmap="Blues", colorbar=False) | |
| axes[1].set_title("Confusion Matrix (Right vs Wrong)") | |
| # 3. ROC Curve | |
| axes[2].plot(fpr, tpr, color='darkorange', lw=2, label=f'ROC curve (AUC = {roc_auc:.2f})') | |
| axes[2].plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--') | |
| axes[2].set_xlim([0.0, 1.0]) | |
| axes[2].set_ylim([0.0, 1.05]) | |
| axes[2].set_xlabel('False Positive Rate') | |
| axes[2].set_ylabel('True Positive Rate') | |
| axes[2].set_title('Receiver Oper. Characteristic (Precision curve)') | |
| axes[2].legend(loc="lower right") | |
| plt.tight_layout() | |
| output_file = "model_evaluation_dashboard.png" | |
| plt.savefig(output_file, dpi=150) | |
| print(f"Successfully generated {output_file}!") | |
| if __name__ == "__main__": | |
| generate_graphs() | |