|
import pandas as pd
|
|
import matplotlib.pyplot as plt
|
|
import seaborn as sns
|
|
from sklearn.model_selection import train_test_split
|
|
from sklearn.ensemble import RandomForestClassifier
|
|
from sklearn.metrics import classification_report, confusion_matrix
|
|
|
|
|
|
|
|
|
|
df = pd.read_csv('feature_engineered_transactions.csv')
|
|
|
|
|
|
|
|
|
|
X = df.drop(columns=['is_anomalous'])
|
|
y = df['is_anomalous']
|
|
|
|
|
|
|
|
|
|
X_train, X_test, y_train, y_test = train_test_split(
|
|
X, y, test_size=0.2, stratify=y, random_state=42
|
|
)
|
|
|
|
|
|
|
|
|
|
clf = RandomForestClassifier(n_estimators=100, random_state=42)
|
|
clf.fit(X_train, y_train)
|
|
|
|
|
|
|
|
|
|
y_pred = clf.predict(X_test)
|
|
|
|
|
|
|
|
|
|
print("\n✅ Classification Report:\n")
|
|
print(classification_report(y_test, y_pred, digits=4))
|
|
|
|
|
|
|
|
|
|
fig, axes = plt.subplots(1, 2, figsize=(15, 6))
|
|
plt.suptitle("Anomaly Detection Results", fontsize=16, fontweight='bold')
|
|
|
|
|
|
cm = confusion_matrix(y_test, y_pred)
|
|
sns.heatmap(
|
|
cm,
|
|
annot=True,
|
|
fmt="d",
|
|
cmap="Blues",
|
|
xticklabels=["Normal", "Suspicious"],
|
|
yticklabels=["Normal", "Suspicious"],
|
|
ax=axes[0]
|
|
)
|
|
axes[0].set_title("Confusion Matrix")
|
|
axes[0].set_xlabel("Predicted")
|
|
axes[0].set_ylabel("Actual")
|
|
|
|
|
|
importances = pd.Series(clf.feature_importances_, index=X.columns).sort_values(ascending=False)
|
|
sns.barplot(
|
|
x=importances.values[:10],
|
|
y=importances.index[:10],
|
|
color='skyblue',
|
|
ax=axes[1]
|
|
)
|
|
axes[1].set_title("Top 10 Feature Importances")
|
|
axes[1].set_xlabel("Importance")
|
|
axes[1].set_ylabel("Feature")
|
|
|
|
|
|
plt.tight_layout(rect=[0, 0, 1, 0.95])
|
|
plt.show()
|
|
|
|
import joblib
|
|
|
|
|
|
joblib.dump(clf, 'anomaly_detector_rf_model.pkl')
|
|
|
|
|
|
joblib.dump(list(X.columns), 'feature_order.pkl')
|
|
|
|
print("✅ Model and feature list saved!")
|
|
|