Update src/streamlit_app.py
Browse files- src/streamlit_app.py +32 -67
src/streamlit_app.py
CHANGED
|
@@ -3,100 +3,65 @@ import pandas as pd
|
|
| 3 |
import joblib
|
| 4 |
import os
|
| 5 |
|
| 6 |
-
# ======================
|
| 7 |
-
# LOAD MODEL
|
| 8 |
-
# ======================
|
| 9 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 10 |
model = joblib.load(os.path.join(BASE_DIR, "model.pkl"))
|
| 11 |
|
| 12 |
-
|
| 13 |
-
# PAGE CONFIG
|
| 14 |
-
# ======================
|
| 15 |
-
st.set_page_config(
|
| 16 |
-
page_title="Porto Seguro Safe Driver Prediction",
|
| 17 |
-
page_icon="π",
|
| 18 |
-
layout="centered"
|
| 19 |
-
)
|
| 20 |
|
| 21 |
st.title("π Porto Seguro β Safe Driver Prediction")
|
| 22 |
-
st.write("Predict the probability that a driver will file an insurance claim.")
|
| 23 |
|
| 24 |
-
|
| 25 |
-
# FRIENDLY NAMES
|
| 26 |
-
# ======================
|
| 27 |
-
friendly_names = {
|
| 28 |
-
"ps_ind_01": "Driver Age",
|
| 29 |
-
"ps_ind_02_cat": "Gender",
|
| 30 |
-
"ps_ind_03": "Driving Experience",
|
| 31 |
-
"ps_ind_04_cat": "Education Level",
|
| 32 |
-
"ps_ind_05_cat": "Employment Type",
|
| 33 |
-
"ps_ind_06_bin": "Has Previous Claims",
|
| 34 |
-
"ps_ind_07_bin": "Owns a Car",
|
| 35 |
-
"ps_ind_08_bin": "Has Driving License",
|
| 36 |
-
"ps_ind_09_bin": "Has Children",
|
| 37 |
-
"ps_ind_10_bin": "Has Insurance History"
|
| 38 |
-
}
|
| 39 |
|
| 40 |
# ======================
|
| 41 |
-
#
|
| 42 |
# ======================
|
| 43 |
-
st.sidebar.header("Driver Information")
|
| 44 |
|
| 45 |
-
|
| 46 |
|
| 47 |
-
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
|
| 51 |
-
|
|
|
|
| 52 |
|
| 53 |
-
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
input_data[feature] = st.sidebar.selectbox(label, [0, 1])
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
|
| 63 |
-
|
| 64 |
-
else:
|
| 65 |
-
input_data[feature] = st.sidebar.number_input(label, value=0.0)
|
| 66 |
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
-
|
| 70 |
-
st.error("Model does not contain feature names.")
|
| 71 |
-
st.stop()
|
| 72 |
|
| 73 |
-
# ======================
|
| 74 |
-
# SHOW INPUT
|
| 75 |
-
# ======================
|
| 76 |
st.subheader("Input Data")
|
| 77 |
st.write(input_df)
|
| 78 |
|
| 79 |
# ======================
|
| 80 |
-
#
|
| 81 |
# ======================
|
| 82 |
-
if st.button("Predict"):
|
| 83 |
-
|
| 84 |
-
prediction = model.predict(input_df)[0]
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
if hasattr(model, "predict_proba"):
|
| 89 |
-
|
| 90 |
-
probability = model.predict_proba(input_df)[0][1]
|
| 91 |
|
| 92 |
-
|
| 93 |
|
| 94 |
-
|
| 95 |
-
st.error("π΄ High Risk Driver")
|
| 96 |
-
elif probability > 0.4:
|
| 97 |
-
st.warning("π‘ Medium Risk Driver")
|
| 98 |
-
else:
|
| 99 |
-
st.success("π’ Low Risk Driver")
|
| 100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
else:
|
| 102 |
-
st.success(
|
|
|
|
| 3 |
import joblib
|
| 4 |
import os
|
| 5 |
|
|
|
|
|
|
|
|
|
|
| 6 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 7 |
model = joblib.load(os.path.join(BASE_DIR, "model.pkl"))
|
| 8 |
|
| 9 |
+
st.set_page_config(page_title="Porto Seguro Prediction", page_icon="π")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
st.title("π Porto Seguro β Safe Driver Prediction")
|
|
|
|
| 12 |
|
| 13 |
+
feature_names = model.feature_names_in_
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
# ======================
|
| 16 |
+
# USER INPUTS (BELANGRIJKSTE)
|
| 17 |
# ======================
|
|
|
|
| 18 |
|
| 19 |
+
st.sidebar.header("Driver Profile")
|
| 20 |
|
| 21 |
+
ps_ind_01 = st.sidebar.slider("Driver Age", 18, 80, 35)
|
| 22 |
+
ps_ind_03 = st.sidebar.slider("Driving Experience", 0, 20, 5)
|
| 23 |
|
| 24 |
+
st.sidebar.header("Insurance History")
|
| 25 |
|
| 26 |
+
ps_ind_06_bin = st.sidebar.selectbox("Has Previous Claims", [0, 1])
|
| 27 |
+
ps_ind_10_bin = st.sidebar.selectbox("Has Insurance History", [0, 1])
|
| 28 |
|
| 29 |
+
st.sidebar.header("Vehicle & License")
|
| 30 |
|
| 31 |
+
ps_ind_07_bin = st.sidebar.selectbox("Owns a Car", [0, 1])
|
| 32 |
+
ps_ind_08_bin = st.sidebar.selectbox("Has Driving License", [0, 1])
|
|
|
|
| 33 |
|
| 34 |
+
# ======================
|
| 35 |
+
# AUTO FILL REST
|
| 36 |
+
# ======================
|
| 37 |
|
| 38 |
+
input_data = dict.fromkeys(feature_names, 0)
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
input_data["ps_ind_01"] = ps_ind_01
|
| 41 |
+
input_data["ps_ind_03"] = ps_ind_03
|
| 42 |
+
input_data["ps_ind_06_bin"] = ps_ind_06_bin
|
| 43 |
+
input_data["ps_ind_10_bin"] = ps_ind_10_bin
|
| 44 |
+
input_data["ps_ind_07_bin"] = ps_ind_07_bin
|
| 45 |
+
input_data["ps_ind_08_bin"] = ps_ind_08_bin
|
| 46 |
|
| 47 |
+
input_df = pd.DataFrame([input_data])
|
|
|
|
|
|
|
| 48 |
|
|
|
|
|
|
|
|
|
|
| 49 |
st.subheader("Input Data")
|
| 50 |
st.write(input_df)
|
| 51 |
|
| 52 |
# ======================
|
| 53 |
+
# PREDICT
|
| 54 |
# ======================
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
+
if st.button("Predict"):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
prob = model.predict_proba(input_df)[0][1]
|
| 59 |
|
| 60 |
+
st.metric("Claim Probability", f"{prob:.2%}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
if prob > 0.7:
|
| 63 |
+
st.error("π΄ High Risk Driver")
|
| 64 |
+
elif prob > 0.4:
|
| 65 |
+
st.warning("π‘ Medium Risk Driver")
|
| 66 |
else:
|
| 67 |
+
st.success("π’ Low Risk Driver")
|