Tryfonas commited on
Commit
e52f3a5
Β·
verified Β·
1 Parent(s): 4096f40

Upload folder using huggingface_hub

Browse files
.gitattributes CHANGED
@@ -1,35 +1,4 @@
1
- *.7z filter=lfs diff=lfs merge=lfs -text
2
- *.arrow filter=lfs diff=lfs merge=lfs -text
3
- *.bin filter=lfs diff=lfs merge=lfs -text
4
- *.bz2 filter=lfs diff=lfs merge=lfs -text
5
- *.ckpt filter=lfs diff=lfs merge=lfs -text
6
- *.ftz filter=lfs diff=lfs merge=lfs -text
7
- *.gz filter=lfs diff=lfs merge=lfs -text
8
- *.h5 filter=lfs diff=lfs merge=lfs -text
9
- *.joblib filter=lfs diff=lfs merge=lfs -text
10
- *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
- *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
- *.model filter=lfs diff=lfs merge=lfs -text
13
- *.msgpack filter=lfs diff=lfs merge=lfs -text
14
- *.npy filter=lfs diff=lfs merge=lfs -text
15
- *.npz filter=lfs diff=lfs merge=lfs -text
16
- *.onnx filter=lfs diff=lfs merge=lfs -text
17
- *.ot filter=lfs diff=lfs merge=lfs -text
18
- *.parquet filter=lfs diff=lfs merge=lfs -text
19
- *.pb filter=lfs diff=lfs merge=lfs -text
20
- *.pickle filter=lfs diff=lfs merge=lfs -text
21
- *.pkl filter=lfs diff=lfs merge=lfs -text
22
- *.pt filter=lfs diff=lfs merge=lfs -text
23
- *.pth filter=lfs diff=lfs merge=lfs -text
24
- *.rar filter=lfs diff=lfs merge=lfs -text
25
- *.safetensors filter=lfs diff=lfs merge=lfs -text
26
- saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
- *.tar.* filter=lfs diff=lfs merge=lfs -text
28
- *.tar filter=lfs diff=lfs merge=lfs -text
29
- *.tflite filter=lfs diff=lfs merge=lfs -text
30
- *.tgz filter=lfs diff=lfs merge=lfs -text
31
- *.wasm filter=lfs diff=lfs merge=lfs -text
32
- *.xz filter=lfs diff=lfs merge=lfs -text
33
- *.zip filter=lfs diff=lfs merge=lfs -text
34
- *.zst filter=lfs diff=lfs merge=lfs -text
35
- *tfevents* filter=lfs diff=lfs merge=lfs -text
 
1
+ kiva_loans.csv filter=lfs diff=lfs merge=lfs -text
2
+ model_xgb.joblib filter=lfs diff=lfs merge=lfs -text
3
+ ohe.joblib filter=lfs diff=lfs merge=lfs -text
4
+ scaler.joblib filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
README.md CHANGED
@@ -1,12 +0,0 @@
1
- ---
2
- title: Module1 Final Assignment
3
- emoji: πŸ“‰
4
- colorFrom: indigo
5
- colorTo: indigo
6
- sdk: streamlit
7
- sdk_version: 1.39.0
8
- app_file: app.py
9
- pinned: false
10
- ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
app.py ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import numpy as np
4
+ import joblib
5
+ import shap
6
+ import xgboost as xgb
7
+
8
+ # Load the saved model and preprocessing objects
9
+ model_xgb = joblib.load('model_xgb.joblib')
10
+ scaler = joblib.load('scaler.joblib')
11
+ ohe = joblib.load('ohe.joblib')
12
+
13
+ # Extract the unique values for 'country' and 'sector' from the OneHotEncoder (ohe) object
14
+ unique_sectors = ohe.categories_[0]
15
+
16
+ # Define a mapping of encoded values to repayment interval labels
17
+ repayment_interval_mapping = {0: 'πŸš… Bullet Repayment Interval', 1: 'πŸͺ™ Irregular Repayment Interval', 2: 'πŸ“… Monthly Repayment Interval'}
18
+
19
+ # Title with emojis and colors
20
+ st.markdown("<h1 style='text-align: center; color: blue;'>πŸ“Š Loan Repayment Interval Prediction for Phillipines(πŸ‡΅πŸ‡­)πŸ“ˆ</h1>", unsafe_allow_html=True)
21
+
22
+ # Input Features Section with emojis and description
23
+ st.write("## 🎯 Input Features")
24
+ st.markdown("Please select the variables to predict the repayment interval based on historical data. The model assumes that all loans used for training were successfully repaid. Kindly provide the following information:")
25
+
26
+ # User input fields using unique country and sector values from the OneHotEncoder object
27
+ sector = st.selectbox('🏒 Sector', unique_sectors)
28
+ funded_amount = st.number_input('πŸ’° Funded Amount', min_value=0, max_value=20000, value=1000, step=50)
29
+ lender_count = st.number_input('πŸ‘₯ Lender Count', min_value=1, max_value=200, value=2, step=1)
30
+
31
+ # Create a sample observation from the user input
32
+ sample_listing = pd.DataFrame({
33
+ 'sector': [sector],
34
+ 'funded_amount': [funded_amount],
35
+ 'lender_count': [lender_count],
36
+ })
37
+
38
+ # Separate categorical and numerical features
39
+ cat_features = ['sector']
40
+ num_features = ['funded_amount', 'lender_count']
41
+
42
+ # Get the feature names from the OneHotEncoder
43
+ ohe_feature_names = ohe.get_feature_names_out(cat_features)
44
+
45
+ # Combine numerical feature names with encoded categorical feature names
46
+ feature_names = np.concatenate([num_features, ohe_feature_names])
47
+
48
+ # One-hot encode categorical features
49
+ X_cat = pd.DataFrame(ohe.transform(sample_listing[cat_features]), columns=ohe_feature_names)
50
+
51
+ # Scale numerical features
52
+ X_num = pd.DataFrame(scaler.transform(sample_listing[num_features]), columns=num_features)
53
+
54
+ # Combine processed features
55
+ X_processed = pd.concat([X_num, X_cat], axis=1)
56
+
57
+ # Make a prediction (returns the encoded value)
58
+ predicted_encoded_repayment_interval = model_xgb.predict(X_processed)[0]
59
+
60
+ # Map the encoded value back to the actual repayment interval label
61
+ predicted_repayment_interval = repayment_interval_mapping.get(int(predicted_encoded_repayment_interval), "Unknown")
62
+
63
+ # Display the actual repayment interval label with more style
64
+ st.title("βœ… Predicted Repayment Interval:")
65
+ st.markdown(f"<h2 style='color:green;'>{predicted_repayment_interval}</h2>", unsafe_allow_html=True)
66
+
67
+ # Explanation for SHAP force plots
68
+ st.write("## πŸ” SHAP Explanation")
69
+ st.markdown("The following SHAP plots explain the model's decision for each repayment interval type. These visualizations help you understand the key features that influenced the model's prediction.")
70
+
71
+ # SHAP explanations
72
+ explainer = shap.TreeExplainer(model_xgb)
73
+ shap_values = explainer.shap_values(X_processed)
74
+
75
+ # Function to add background color to SHAP plots
76
+ def add_background(html_content):
77
+ white_background_style = "<style>body { background-color: white; }</style>"
78
+ return white_background_style + html_content
79
+
80
+ # Generate and save SHAP force plot for Class 0 (Bullet)
81
+ st.write("### πŸš… SHAP Force Plot for Class 0: Bullet Repayment Interval")
82
+ st.markdown("This plot explains the factors influencing the Bullet repayment interval prediction. Bullet repayment means paying off the loan in one lump sum at the end of the loan period.")
83
+ shap_html_path_0 = "shap_force_plot_class_0.html"
84
+ shap.save_html(shap_html_path_0, shap.force_plot(
85
+ explainer.expected_value[0],
86
+ shap_values[0][:, 0],
87
+ X_processed.iloc[0, :].values,
88
+ feature_names,
89
+ show=False,
90
+ matplotlib=False
91
+ ))
92
+ with open(shap_html_path_0, 'r', encoding='utf-8') as f:
93
+ shap_html_0 = f.read()
94
+ st.components.v1.html(add_background(shap_html_0), height=130)
95
+
96
+ # Generate and save SHAP force plot for Class 1 (Irregular)
97
+ st.write("### πŸͺ™ SHAP Force Plot for Class 1: Irregular Repayment Interval")
98
+ st.markdown("This plot explains the factors influencing the Irregular repayment interval prediction. Irregular repayment means paying off the loan at irregular intervals based on specific conditions.")
99
+ shap_html_path_1 = "shap_force_plot_class_1.html"
100
+ shap.save_html(shap_html_path_1, shap.force_plot(
101
+ explainer.expected_value[1],
102
+ shap_values[0][:, 1],
103
+ X_processed.iloc[0, :].values,
104
+ feature_names,
105
+ show=False,
106
+ matplotlib=False
107
+ ))
108
+ with open(shap_html_path_1, 'r', encoding='utf-8') as f:
109
+ shap_html_1 = f.read()
110
+ st.components.v1.html(add_background(shap_html_1), height=130)
111
+
112
+ # Generate and save SHAP force plot for Class 2 (Monthly)
113
+ st.write("### πŸ“… SHAP Force Plot for Class 2: Monthly Repayment Interval")
114
+ st.markdown("This plot explains the factors influencing the Monthly repayment interval prediction. Monthly repayment means paying off the loan in equal monthly installments.")
115
+ shap_html_path_2 = "shap_force_plot_class_2.html"
116
+ shap.save_html(shap_html_path_2, shap.force_plot(
117
+ explainer.expected_value[2],
118
+ shap_values[0][:, 2],
119
+ X_processed.iloc[0, :].values,
120
+ feature_names,
121
+ show=False,
122
+ matplotlib=False
123
+ ))
124
+ with open(shap_html_path_2, 'r', encoding='utf-8') as f:
125
+ shap_html_2 = f.read()
126
+ st.components.v1.html(add_background(shap_html_2), height=131)
kiva_loans.csv ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b20efc20de600b27608d69fe07e728b00a075c3db29849e146b717098f778d92
3
+ size 195852823
model_xgb.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:73770dd9def66adf968ed2e60a9d864f6b917818be6542f918f717ddfe2563c3
3
+ size 1973020
ohe.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2550ee4d74a18082c90de8d363b07ba628444738aafc2a8bab5a3e66337ee306
3
+ size 1300
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+
2
+ streamlit
3
+ pandas
4
+ numpy
5
+ xgboost
6
+ scikit-learn
7
+ shap
8
+ matplotlib
9
+ joblib
scaler.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0c7aba2e0f726f03a51729ece44fd2092ae3198a392eb8f1b7721366489d8d15
3
+ size 999
shap_force_plot_class_0.html ADDED
The diff for this file is too large to render. See raw diff
 
shap_force_plot_class_1.html ADDED
The diff for this file is too large to render. See raw diff
 
shap_force_plot_class_2.html ADDED
The diff for this file is too large to render. See raw diff