-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
164 lines (132 loc) · 6.05 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import pandas as pd
import streamlit as st
from keras.models import load_model
from embeddings import One_Hot_Encoder
from PIL import Image
import pickle
import xgboost as xgb
categorical_columns = [
"Country",
"Sector",
"Activity",
"Local Currency",
]
low_risk_image = Image.open('images/lowrisk.JPG')
high_risk_image = Image.open('images/highrisk.JPG')
model_infrence = Image.open('images/shap.JPG')
ordinal_columns = ["Loan Amount"]
@st.cache
#streamlit cached funtion to load ds into web app
def load_ds():
rdf = pd.read_csv("data/BigML_Dataset.csv")
st.header('Raw Dataset')
return rdf
#funtion to load sidebar which contains all the features that the lending institution will input to get risk assesment
def load_sidebar(raw_df):
arg0 = st.sidebar.selectbox('Which ML Model would you like to run?', models)
arg1 = st.sidebar.slider("Please Select the Loan Amount", 100, 2000)
arg2 = st.sidebar.selectbox("Please Select the Applicant Country", country_list)
arg5 = st.sidebar.selectbox("Please Select the Local Currency", currency_list)
arg3 = st.sidebar.selectbox("Please Select the Applicant Activity", activity_list)
arg4 = st.sidebar.selectbox("Please Select the Applicant Sector", sector_list)
b1 = st.sidebar.button("Get Predictions", key=1)
b2 = st.button("XGB Model Infrence", key=1)
return [arg0, arg1, arg2, arg3, arg4, arg5], [b1, b2]
# to load saved machine learning models using pickle from saved models directory
def fetch_model(model_name):
loaded_model = pickle.load(open('saved_models/'+model_name+'.sav', 'rb'))
return loaded_model
# get a empty sample_df which can be fitted with all the input values
def get_sampledf(args):
input_df = pd.read_csv("data/sample_df.csv")
ColNameList = ["Country_"+args[2],"Local Currency_"+args[5],"Activity_"+args[3],"Sector_"+args[4]]
input_df.loc[0, ColNameList] = 1
input_df.loc[0, "Loan Amount"] = arguments[1]
return input_df
# load images on the application based on the result of the selected machine learning model
def show_risk_image(result):
if int(result) == 0:
st.image(low_risk_image, caption='Default Risk', use_column_width=True)
else:
st.image(high_risk_image, caption='Default Risk', use_column_width=True)
st.title('Loan Safe : ML powered Risk Assesment ')
st.header("Loading Big ML Dataset...")
raw_df = load_ds()
country_list = raw_df['Country'].value_counts().index.tolist()
activity_list = raw_df['Activity'].value_counts().index.tolist()
sector_list = raw_df['Sector'].value_counts().index.tolist()
currency_list = raw_df['Local Currency'].value_counts().index.tolist()
models = ["Random Forrest", "Logistic Regression", "XGBoost", "Neural Network",
"Deep Neural Network w/ pretrained Embeddings"]
st.subheader("Dataset Retrieved!")
st.dataframe(raw_df.head(10))
arguments, buttons = load_sidebar(raw_df)
max_length = 1
# if conditional tree to drive code based on buttons clicked on the streamlit web application which calls the above defined funtions to make predictions
# using the machine learning model
if buttons[0] and arguments[0] == "Neural Network":
st.subheader("Fetching Neural Network Model")
model = load_model('saved_models/plain_nn')
print("model loaded")
df_sample = pd.read_csv("data/sample_dataset.csv")
df_sample.loc[0, 'Country'], df_sample.loc[0, 'Local Currency'], df_sample.loc[0, 'Loan Amount'], df_sample.loc[
0, 'Activity'], df_sample.loc[0, 'Sector'] = arguments[2], arguments[5], arguments[1], arguments[3], arguments[4]
categorical_encoder_demo = One_Hot_Encoder(df_sample, categorical_columns)
categorical_data_demo = categorical_encoder_demo.encode(
df_sample, categorical_columns, max_length
)
ordinal_data_demo = [
df_sample[c]
for c in ordinal_columns
]
imput_data_demo = categorical_data_demo + ordinal_data_demo
input_data_demo_test = [data for data in imput_data_demo]
y_pred = []
y_pred.append(model.predict(input_data_demo_test))
st.header("Based on Data Provided...")
show_risk_image(y_pred[0])
elif buttons[0] and arguments[0] == "Deep Neural Network w/ pretrained Embeddings":
st.subheader("Fetching Deep Neural Network Model")
model = load_model('saved_models/deep_nn')
print("model loaded")
df_sample = pd.read_csv("data/sample_dataset.csv")
df_sample.loc[0, 'Country'], df_sample.loc[0, 'Local Currency'], df_sample.loc[0, 'Loan Amount'], df_sample.loc[
0, 'Activity'], df_sample.loc[0, 'Sector'] = arguments[2], arguments[5], arguments[1], arguments[3], arguments[4]
categorical_encoder_demo = One_Hot_Encoder(df_sample, categorical_columns)
categorical_data_demo = categorical_encoder_demo.encode(
df_sample, categorical_columns, max_length
)
ordinal_data_demo = [
df_sample[c]
for c in ordinal_columns
]
imput_data_demo = categorical_data_demo + ordinal_data_demo
input_data_demo_test = [data for data in imput_data_demo]
y_pred = []
y_pred.append(model.predict(input_data_demo_test))
st.header("Based on Data Provided...")
show_risk_image(y_pred[0])
elif buttons[0] and arguments[0] == "Random Forrest":
st.subheader("Fetching Random Forrest Model")
rand_forrest = fetch_model("rand_forrest")
x = get_sampledf(arguments)
res=rand_forrest.predict(x)
st.header("Based on Data Provided...")
show_risk_image(res[0])
elif buttons[0] and arguments[0] == "Logistic Regression":
st.subheader("Fetching Logistic Regression Model")
log_reg = fetch_model("log_reg")
x = get_sampledf(arguments)
res=log_reg.predict(x)
st.header("Based on Data Provided...")
show_risk_image(res[0])
elif buttons[0] and arguments[0] == "XGBoost":
st.subheader("Fetching XGBoost Model")
xgbm = fetch_model("xgboost")
x = get_sampledf(arguments)
dmat = xgb.DMatrix(data=x)
res=xgbm.predict(dmat)[0]
st.header("Based on Data Provided...")
show_risk_image(1 if res>0.5 else 0)
if buttons[1]:
st.image(model_infrence, caption='SHAP Summary Plot', use_column_width=True)