-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
79 lines (65 loc) · 3.04 KB
/
Copy pathapp.py
File metadata and controls
79 lines (65 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# app.py
import streamlit as st
import pickle
import pandas as pd
import joblib
# Load model and encoder
try:
with open('model/Grad_Boost.sav', 'rb') as model_file:
loaded_model = pickle.load(model_file) # trained GradientBoost model
with open('PreProcessing/encoders.pkl', 'rb') as encoder_file:
loaded_encoder = joblib.load(encoder_file) # trained TargetEncoder
st.success("✅ Model and encoder loaded successfully!")
except Exception as e:
st.error(f"Error loading model/encoder: {e}")
st.title("🚨 Claim Fraud Detection")
# Define input features (MUST match training)
features = [
'months_as_customer','policy_state','policy_csl','policy_deductable',
'policy_annual_premium','umbrella_limit','insured_zip','insured_sex',
'insured_education_level','insured_occupation','insured_hobbies',
'insured_relationship','capital-gains','capital-loss','incident_type',
'collision_type','incident_severity','authorities_contacted',
'incident_state','incident_city','incident_hour_of_the_day',
'number_of_vehicles_involved','property_damage','bodily_injuries',
'witnesses','police_report_available','total_claim_amount',
'injury_claim','property_claim','auto_make','auto_year',
'incident_month','incident_day','auto_model'
]
st.sidebar.header("Input Features")
# Collect user inputs
input_data = {}
for feature in features:
if feature in [
'policy_state','policy_csl','insured_sex','insured_education_level',
'insured_occupation','insured_hobbies','insured_relationship',
'incident_type','collision_type','incident_severity','authorities_contacted',
'incident_state','incident_city','property_damage','police_report_available',
'auto_make','auto_model'
]:
input_data[feature] = st.sidebar.text_input(f"{feature}")
else:
input_data[feature] = st.sidebar.number_input(f"{feature}", value=0.0)
# Predict button
if st.sidebar.button("Predict Fraud"):
# Convert to DataFrame
input_df = pd.DataFrame([input_data])
# Apply trained encoder ONLY on categorical cols
categorical_cols = [
'auto_model','policy_state','incident_type','auto_make','police_report_available',
'property_damage','incident_city','incident_state','authorities_contacted',
'incident_severity','collision_type','insured_relationship','insured_hobbies',
'insured_occupation','insured_education_level','insured_sex','policy_csl'
]
# Encode categorical features
for col, te in loaded_encoder.items():
input_df[col] = te.transform(input_df[[col]])
# Align columns with model training
expected_cols = loaded_model.feature_names_in_
input_df = input_df.reindex(columns=expected_cols, fill_value=0)
# Predict
prediction = loaded_model.predict(input_df)[0]
prediction_proba = loaded_model.predict_proba(input_df)[0][1]
st.subheader("🔎 Prediction Result")
st.write("**Fraudulent Claim** 🚩" if prediction == 1 else "✅ Not Fraudulent")
st.write(f"Fraud Probability: {prediction_proba:.2f}")