Update app.py
Browse files
app.py
CHANGED
@@ -5,21 +5,25 @@ import gradio as gr
|
|
5 |
# Load the trained model
|
6 |
model = joblib.load("anomaly_detector_rf_model.pkl")
|
7 |
|
8 |
-
# Define feature columns (
|
9 |
-
feature_cols = ['hour', 'day_of_week', 'is_weekend', '
|
10 |
-
'
|
|
|
11 |
|
12 |
def detect_anomalies(file_path):
|
13 |
# Read the dataset
|
14 |
df = pd.read_csv(file_path)
|
15 |
|
16 |
# Ensure all features exist in the dataframe
|
17 |
-
|
18 |
-
|
19 |
return f"Missing columns in dataset: {missing_cols}"
|
20 |
|
|
|
|
|
|
|
21 |
# Make predictions
|
22 |
-
df['is_anomalous'] = model.predict(df
|
23 |
|
24 |
# Filter anomalous transactions
|
25 |
anomalies = df[df['is_anomalous'] == 1][['transaction_id', 'merchant', 'location', 'amount']]
|
|
|
5 |
# Load the trained model
|
6 |
model = joblib.load("anomaly_detector_rf_model.pkl")
|
7 |
|
8 |
+
# Define feature columns (include all used during training)
|
9 |
+
feature_cols = ['hour', 'day_of_week', 'is_weekend', 'amount', 'merchant_avg_amount',
|
10 |
+
'amount_zscore', 'log_amount', 'type_atm_withdrawal', 'type_credit',
|
11 |
+
'type_debit', 'merchant_encoded']
|
12 |
|
13 |
def detect_anomalies(file_path):
|
14 |
# Read the dataset
|
15 |
df = pd.read_csv(file_path)
|
16 |
|
17 |
# Ensure all features exist in the dataframe
|
18 |
+
missing_cols = [col for col in feature_cols if col not in df.columns]
|
19 |
+
if missing_cols:
|
20 |
return f"Missing columns in dataset: {missing_cols}"
|
21 |
|
22 |
+
# Align feature order with model training
|
23 |
+
df = df[feature_cols]
|
24 |
+
|
25 |
# Make predictions
|
26 |
+
df['is_anomalous'] = model.predict(df)
|
27 |
|
28 |
# Filter anomalous transactions
|
29 |
anomalies = df[df['is_anomalous'] == 1][['transaction_id', 'merchant', 'location', 'amount']]
|