AdisJulardzija commited on
Commit
a84a568
·
verified ·
1 Parent(s): e22f445

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -0
app.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import numpy as np
4
+ import joblib
5
+ from xgboost import XGBRegressor
6
+ from sklearn.preprocessing import StandardScaler, OneHotEncoder
7
+ import shap
8
+ from streamlit_shap import st_shap
9
+
10
+
11
+
12
+ # Page configuration
13
+ st.set_page_config(
14
+ page_title="Medical Costs Concern Prediction",)
15
+
16
+ st.title('Predict Medical Costs Concern')
17
+
18
+ # Load model and preprocessing objects
19
+ @st.cache_resource
20
+ def load_model_objects():
21
+ model_xgb = joblib.load('model_best.joblib')
22
+ scaler = joblib.load('scaler.joblib')
23
+ return model_xgb, scaler
24
+
25
+ model_xgb, scaler = load_model_objects()
26
+ # Create SHAP explainer
27
+ explainer = shap.TreeExplainer(model_xgb)
28
+
29
+ # App description
30
+ with st.expander("What's this app?"):
31
+ st.markdown("""
32
+ This app predicts how worried a person is about medical costs, based on factors like age, education, income, and employment status.
33
+ We've trained an AI model to analyze these inputs and give a prediction.
34
+ """)
35
+
36
+ st.subheader('Describe yourself')
37
+
38
+ # User inputs
39
+ col1, col2 = st.columns(2)
40
+
41
+ with col1:
42
+ age = st.number_input('Age', min_value=18, max_value=100, value=30)
43
+ education = st.selectbox('Education Level', options=['Primary', 'Secondary', 'Tertiary'], index=1)
44
+ income_quartile = st.radio('Income Quartile', options=['Lowest', 'Second', 'Third', 'Highest'])
45
+
46
+ with col2:
47
+ employment_status = st.selectbox('Employment Status', options=['Unemployed', 'Employed'], index=1)
48
+
49
+ # Map user inputs to numerical and categorical features
50
+ education_mapping = {'Primary': 1, 'Secondary': 2, 'Tertiary': 3}
51
+ income_mapping = {'Lowest': 1, 'Second': 2, 'Third': 3, 'Highest': 4}
52
+ employment_mapping = {'Unemployed': 0, 'Employed': 1}
53
+
54
+ # Transform user input into a feature vector
55
+ education_num = education_mapping[education]
56
+ income_num = income_mapping[income_quartile]
57
+ employment_num = employment_mapping[employment_status]
58
+
59
+ # Prepare features for the model
60
+ num_features = pd.DataFrame({
61
+ 'age': [age],
62
+ 'educ': [education_num],
63
+ 'inc_q': [income_num],
64
+ 'emp_in': [employment_num]
65
+ })
66
+ num_scaled = pd.DataFrame(scaler.transform(num_features), columns=num_features.columns)
67
+
68
+ # Prediction button
69
+ if st.button('Predict Concern Level'):
70
+ # Make prediction
71
+ predicted_concern = model_xgb.predict(num_scaled)[0]
72
+ # Display prediction
73
+ st.metric(label="Predicted concern level", value=f'{round(predicted_concern)} (1: Not Worried, 3: Very Worried)')
74
+ # SHAP explanation
75
+ st.subheader('Concern Factors Explained')
76
+ shap_values = explainer.shap_values(num_scaled)
77
+ st_shap(shap.force_plot(explainer.expected_value, shap_values, num_scaled), height=400, width=600)
78
+ st.markdown("""
79
+ This plot shows how each feature contributes to the predicted concern level:
80
+ - Blue bars push the concern level lower
81
+ - Red bars push the concern level higher
82
+ - The length of each bar indicates the strength of the feature's impact
83
+ """)
84
+
85
+ # Footer
86
+ st.markdown("---")