Spaces:
Sleeping
Sleeping
Update app.py
Browse files445 unmodified lines4464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055064 unmodified lines511512513514515516517445 unmodified lineselse:st.write("Click the 'Get Recommendations' button to receive personalized financial recommendations.")elif option == "SML Classification": # This is the section you want to display content onst.markdown("<h2 style='text-align: center;'>SML Classification</h2>", unsafe_allow_html=True)st.write("This section allows you to predict savings behavior based on multiple variables.")st.subheader('Enter your information')# Categorical Inputsplace_of_living = st.selectbox('Place of Living', ['Urban Area', 'Rural Area', 'Unknown'])education_level = st.selectbox('Education Level', ['Primary', 'Secondary', 'Tertiary'])age_group = st.selectbox('Age Group', ['Adult', 'Middle Age', 'Older Adult', 'Senior', 'Teen', 'Young Adult'])income_bracket = st.selectbox('Income Bracket', [1, 2, 3, 4, 5])# Binary Inputsfemale = st.radio('Gender', ['Female', 'Male'])is_mobileowner = st.checkbox('Is Mobile Owner?')has_internet_access = st.checkbox('Has Internet Access?')employed = st.checkbox('Employed?')high_income_region = st.checkbox('High Income Region?')# Prediction buttonif st.button('Predict Saving Behavior'):# Prepare categorical featurescat_features = pd.DataFrame({'Place of living': [place_of_living],'Education Level': [education_level],'Age Group': [age_group],'Income Bracket': [income_bracket]})# Transform categorical features using the loaded OneHotEncodercat_encoded = pd.DataFrame(ohe.transform(cat_features).todense(),columns=ohe.get_feature_names_out(['Place of living', 'Education Level', 'Age Group', 'Income Bracket']))# Ensure all expected columns are presentexpected_columns = ohe.get_feature_names_out(['Place of living', 'Education Level', 'Age Group', 'Income Bracket'])for col in expected_columns:if col not in cat_encoded.columns:cat_encoded[col] = 0 # Add missing columns with a default value of 0# Reorder columns to match the expected ordercat_encoded = cat_encoded[expected_columns]# Prepare binary and numerical featuresnum_features = pd.DataFrame({'Female': [1 if female == 'Female' else 0],'Is Mobileowner': [1 if is_mobileowner else 0],'Has Internet Access': [1 if has_internet_access else 0],'Employed': [1 if employed else 0],'High Income Region': [1 if high_income_region else 0]})# Combine categorical and numerical featuresfeatures = pd.concat([num_features, cat_encoded], axis=1)# Make the predictionpredicted_saved = xgb_model.predict(features)[0]4 unmodified linesst.subheader('Feature Contributions 🤖')shap_values = explainer.shap_values(features)st_shap(shap.force_plot(explainer.expected_value, shap_values, features), height=400, width=600)st.markdown("""This plot shows how each feature contributes to the predicted likelihood of saving:- Blue bars push the probability lower445 unmodified lines4464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904 unmodified lines495496497498499500501445 unmodified lineselse:st.write("Click the 'Get Recommendations' button to receive personalized financial recommendations.")elif option == "SML Classification":st.markdown("<h2 style='text-align: center;'>SML Classification</h2>", unsafe_allow_html=True)# Collect user inputsplace_of_living = st.selectbox('Place of Living', ['Urban Area', 'Rural Area', 'Unknown'])education_level = st.selectbox('Education Level', ['Primary', 'Secondary', 'Tertiary'])age_group = st.selectbox('Age Group', ['Adult', 'Middle Age', 'Older Adult', 'Senior', 'Teen', 'Young Adult'])income_bracket = st.selectbox('Income Bracket', [1, 2, 3, 4, 5])female = st.radio('Gender', ['Female', 'Male'])is_mobileowner = st.checkbox('Is Mobile Owner?')has_internet_access = st.checkbox('Has Internet Access?')employed = st.checkbox('Employed?')high_income_region = st.checkbox('High Income Region?')# Prepare categorical and numerical featurescat_features = pd.DataFrame({'Place of living': [place_of_living],'Education Level': [education_level],'Age Group': [age_group]})num_features = pd.DataFrame({'Female': [1 if female == 'Female' else 0],'Is Mobileowner': [1 if is_mobileowner else 0],'Has Internet Access': [1 if has_internet_access else 0],'Employed': [1 if employed else 0],'High Income Region': [1 if high_income_region else 0],'Income Bracket': [income_bracket] # Directly use Income Bracket as numerical})# OneHotEncode only the categorical variablescat_encoded = pd.DataFrame(ohe.transform(cat_features).todense(),columns=ohe.get_feature_names_out(['Place of living', 'Education Level', 'Age Group']))# Combine categorical and numerical featuresfeatures = pd.concat([num_features, cat_encoded], axis=1)# Prediction buttonif st.button('Predict Saving Behavior'):# Make the predictionpredicted_saved = xgb_model.predict(features)[0]4 unmodified linesst.subheader('Feature Contributions 🤖')shap_values = explainer.shap_values(features)st_shap(shap.force_plot(explainer.expected_value, shap_values, features), height=400, width=600)st.markdown("""This plot shows how each feature contributes to the predicted likelihood of saving:- Blue bars push the probability lower
app.py+26-42