Bjerring98 commited on
Commit
1cf5881
·
verified ·
1 Parent(s): 2b19c7c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -42
app.py CHANGED
@@ -446,61 +446,45 @@ elif option == "Financial Recommender Engine":
446
  else:
447
  st.write("Click the 'Get Recommendations' button to receive personalized financial recommendations.")
448
 
449
- elif option == "SML Classification": # This is the section you want to display content on
450
  st.markdown("<h2 style='text-align: center;'>SML Classification</h2>", unsafe_allow_html=True)
451
-
452
- st.write("This section allows you to predict savings behavior based on multiple variables.")
453
-
454
- st.subheader('Enter your information')
455
 
456
- # Categorical Inputs
457
  place_of_living = st.selectbox('Place of Living', ['Urban Area', 'Rural Area', 'Unknown'])
458
  education_level = st.selectbox('Education Level', ['Primary', 'Secondary', 'Tertiary'])
459
  age_group = st.selectbox('Age Group', ['Adult', 'Middle Age', 'Older Adult', 'Senior', 'Teen', 'Young Adult'])
460
  income_bracket = st.selectbox('Income Bracket', [1, 2, 3, 4, 5])
461
-
462
- # Binary Inputs
463
  female = st.radio('Gender', ['Female', 'Male'])
464
  is_mobileowner = st.checkbox('Is Mobile Owner?')
465
  has_internet_access = st.checkbox('Has Internet Access?')
466
  employed = st.checkbox('Employed?')
467
  high_income_region = st.checkbox('High Income Region?')
468
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
469
  # Prediction button
470
  if st.button('Predict Saving Behavior'):
471
- # Prepare categorical features
472
- cat_features = pd.DataFrame({
473
- 'Place of living': [place_of_living],
474
- 'Education Level': [education_level],
475
- 'Age Group': [age_group],
476
- 'Income Bracket': [income_bracket]
477
- })
478
-
479
- # Transform categorical features using the loaded OneHotEncoder
480
- cat_encoded = pd.DataFrame(ohe.transform(cat_features).todense(),
481
- columns=ohe.get_feature_names_out(['Place of living', 'Education Level', 'Age Group', 'Income Bracket']))
482
-
483
- # Ensure all expected columns are present
484
- expected_columns = ohe.get_feature_names_out(['Place of living', 'Education Level', 'Age Group', 'Income Bracket'])
485
- for col in expected_columns:
486
- if col not in cat_encoded.columns:
487
- cat_encoded[col] = 0 # Add missing columns with a default value of 0
488
-
489
- # Reorder columns to match the expected order
490
- cat_encoded = cat_encoded[expected_columns]
491
-
492
- # Prepare binary and numerical features
493
- num_features = pd.DataFrame({
494
- 'Female': [1 if female == 'Female' else 0],
495
- 'Is Mobileowner': [1 if is_mobileowner else 0],
496
- 'Has Internet Access': [1 if has_internet_access else 0],
497
- 'Employed': [1 if employed else 0],
498
- 'High Income Region': [1 if high_income_region else 0]
499
- })
500
-
501
- # Combine categorical and numerical features
502
- features = pd.concat([num_features, cat_encoded], axis=1)
503
-
504
  # Make the prediction
505
  predicted_saved = xgb_model.predict(features)[0]
506
 
@@ -511,7 +495,7 @@ elif option == "SML Classification": # This is the section you want to display
511
  st.subheader('Feature Contributions 🤖')
512
  shap_values = explainer.shap_values(features)
513
  st_shap(shap.force_plot(explainer.expected_value, shap_values, features), height=400, width=600)
514
-
515
  st.markdown("""
516
  This plot shows how each feature contributes to the predicted likelihood of saving:
517
  - Blue bars push the probability lower
 
446
  else:
447
  st.write("Click the 'Get Recommendations' button to receive personalized financial recommendations.")
448
 
449
+ elif option == "SML Classification":
450
  st.markdown("<h2 style='text-align: center;'>SML Classification</h2>", unsafe_allow_html=True)
 
 
 
 
451
 
452
+ # Collect user inputs
453
  place_of_living = st.selectbox('Place of Living', ['Urban Area', 'Rural Area', 'Unknown'])
454
  education_level = st.selectbox('Education Level', ['Primary', 'Secondary', 'Tertiary'])
455
  age_group = st.selectbox('Age Group', ['Adult', 'Middle Age', 'Older Adult', 'Senior', 'Teen', 'Young Adult'])
456
  income_bracket = st.selectbox('Income Bracket', [1, 2, 3, 4, 5])
 
 
457
  female = st.radio('Gender', ['Female', 'Male'])
458
  is_mobileowner = st.checkbox('Is Mobile Owner?')
459
  has_internet_access = st.checkbox('Has Internet Access?')
460
  employed = st.checkbox('Employed?')
461
  high_income_region = st.checkbox('High Income Region?')
462
 
463
+ # Prepare categorical and numerical features
464
+ cat_features = pd.DataFrame({
465
+ 'Place of living': [place_of_living],
466
+ 'Education Level': [education_level],
467
+ 'Age Group': [age_group]
468
+ })
469
+
470
+ num_features = pd.DataFrame({
471
+ 'Female': [1 if female == 'Female' else 0],
472
+ 'Is Mobileowner': [1 if is_mobileowner else 0],
473
+ 'Has Internet Access': [1 if has_internet_access else 0],
474
+ 'Employed': [1 if employed else 0],
475
+ 'High Income Region': [1 if high_income_region else 0],
476
+ 'Income Bracket': [income_bracket] # Directly use Income Bracket as numerical
477
+ })
478
+
479
+ # OneHotEncode only the categorical variables
480
+ cat_encoded = pd.DataFrame(ohe.transform(cat_features).todense(),
481
+ columns=ohe.get_feature_names_out(['Place of living', 'Education Level', 'Age Group']))
482
+
483
+ # Combine categorical and numerical features
484
+ features = pd.concat([num_features, cat_encoded], axis=1)
485
+
486
  # Prediction button
487
  if st.button('Predict Saving Behavior'):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
488
  # Make the prediction
489
  predicted_saved = xgb_model.predict(features)[0]
490
 
 
495
  st.subheader('Feature Contributions 🤖')
496
  shap_values = explainer.shap_values(features)
497
  st_shap(shap.force_plot(explainer.expected_value, shap_values, features), height=400, width=600)
498
+
499
  st.markdown("""
500
  This plot shows how each feature contributes to the predicted likelihood of saving:
501
  - Blue bars push the probability lower