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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
## IMPORTING LIBS
|
| 2 |
+
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import plotly.express as px
|
| 5 |
+
|
| 6 |
+
from sklearn.metrics import f1_score, precision_score, recall_score, confusion_matrix
|
| 7 |
+
|
| 8 |
+
import pickle
|
| 9 |
+
import gradio as gr
|
| 10 |
+
|
| 11 |
+
## CREATING FUNCTION
|
| 12 |
+
|
| 13 |
+
def predict_credit_worthiness(name, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18, x19, x20, x21, x22):
|
| 14 |
+
path = 'german_credit_risk/model/model.pickle'
|
| 15 |
+
greet = 'Hey, ' + name + '!'
|
| 16 |
+
with open(path, 'rb') as file:
|
| 17 |
+
model = pickle.load(file)
|
| 18 |
+
inputs = {'Account Balance_1': int(x1),
|
| 19 |
+
'Account Balance_2': int(x2),
|
| 20 |
+
'Account Balance_3': int(x3),
|
| 21 |
+
'Payment Status of Previous Credit_1': int(x4),
|
| 22 |
+
'Payment Status of Previous Credit_3': int(x5),
|
| 23 |
+
'Purpose_1': int(x6),
|
| 24 |
+
'Purpose_4': int(x7),
|
| 25 |
+
'Value Savings/Stocks_1': int(x8),
|
| 26 |
+
'Value Savings/Stocks_3': int(x9),
|
| 27 |
+
'Value Savings/Stocks_5': int(x10),
|
| 28 |
+
'Length of current employment_1': int(x11),
|
| 29 |
+
'Length of current employment_4': int(x12),
|
| 30 |
+
'Instalment per cent_4': int(x13),
|
| 31 |
+
'Guarantors_1': int(x14),
|
| 32 |
+
'Duration in Current address_1': int(x15),
|
| 33 |
+
'Duration in Current address_2': int(x16),
|
| 34 |
+
'Most valuable available asset_1': int(x17),
|
| 35 |
+
'Most valuable available asset_4': int(x18),
|
| 36 |
+
'Concurrent Credits_3': int(x19),
|
| 37 |
+
'Type of apartment_1': int(x20),
|
| 38 |
+
'No of Credits at this Bank_1': int(x21),
|
| 39 |
+
'Occupation_1': int(x22)
|
| 40 |
+
}
|
| 41 |
+
prediction = model.predict([list(inputs.values())])
|
| 42 |
+
|
| 43 |
+
y_test = pd.read_parquet('german_credit_risk/data/processed/y_test.parquet')
|
| 44 |
+
y_test = y_test.squeeze()
|
| 45 |
+
|
| 46 |
+
yhat = pd.read_parquet('german_credit_risk/data/processed/yhat.parquet')
|
| 47 |
+
yhat = yhat.squeeze()
|
| 48 |
+
|
| 49 |
+
precision = precision_score(y_test, yhat).round(2)
|
| 50 |
+
recall = recall_score(y_test, yhat).round(2)
|
| 51 |
+
f1 = f1_score(y_test, yhat).round(2)
|
| 52 |
+
|
| 53 |
+
features_names = ['No account', 'No balance', 'Some balance', 'No credit problems',
|
| 54 |
+
'Some credit problems', 'New car', 'Other purpose', 'No savings',
|
| 55 |
+
'DM betwenn [100, 1000]', 'DM >= 1000', 'Employment: <1 year (or unemployed)', 'Employment: 4<x<7 years',
|
| 56 |
+
'Installment smaller than 20%', 'No guarantors', 'Less than a year in same address', '1<x<4 years in address',
|
| 57 |
+
'Not available / no assets', 'Ownership of house or land', 'No further running credits', 'Free ap',
|
| 58 |
+
'One credit at thins bank','Unemployed or unskilled']
|
| 59 |
+
importance = model.feature_importances_
|
| 60 |
+
data = pd.DataFrame()
|
| 61 |
+
data['Feature Importance'] = importance
|
| 62 |
+
data['Feature'] = features_names
|
| 63 |
+
p = px.bar(data, y='Feature Importance', x='Feature', width=1200, height=500)
|
| 64 |
+
|
| 65 |
+
cfm = confusion_matrix(y_test, yhat)
|
| 66 |
+
cfm_plot = px.imshow(cfm,
|
| 67 |
+
x=['Predicted 0', 'Predicted 1'],
|
| 68 |
+
y=['Actual 0', 'Actual 1'],
|
| 69 |
+
color_continuous_scale='Blues',
|
| 70 |
+
labels=dict(x="Predicted", y="Actual", color="Count"),
|
| 71 |
+
text_auto=True)
|
| 72 |
+
|
| 73 |
+
if prediction == 1:
|
| 74 |
+
return (greet + ' According to our model, your client is eligible for the loan.',
|
| 75 |
+
'Precision: '+ str(precision),
|
| 76 |
+
'Recall: '+ str(recall),
|
| 77 |
+
'F1 Score: '+ str(f1),
|
| 78 |
+
p,
|
| 79 |
+
cfm_plot)
|
| 80 |
+
else:
|
| 81 |
+
return (greet + ' Unfortunately, according to our model, your client is not eligible for the loan for now :(.',
|
| 82 |
+
'Precision: '+ str(precision),
|
| 83 |
+
'Recall: '+ str(recall),
|
| 84 |
+
'F1 Score: '+ str(f1),
|
| 85 |
+
p,
|
| 86 |
+
cfm_plot)
|
| 87 |
+
|
| 88 |
+
## creating the interface
|
| 89 |
+
|
| 90 |
+
with gr.Blocks() as demo:
|
| 91 |
+
gr.Markdown('# Credit Worthiness Prediction')
|
| 92 |
+
gr.Markdown("""
|
| 93 |
+
To predict our clients' creditworthiness, please use this application as follows:
|
| 94 |
+
|
| 95 |
+
1. Enter your name and navigate through the client's information tabs. Select the boxes that best match your client's characteristics. Leave blank if none apply.
|
| 96 |
+
|
| 97 |
+
2. Once completed, click 'Predict' to determine if the client is creditworthy.
|
| 98 |
+
""")
|
| 99 |
+
with gr.Accordion('Name'):
|
| 100 |
+
name = gr.Textbox(lines=1, label='Your name')
|
| 101 |
+
with gr.Accordion("Enter your client's information"):
|
| 102 |
+
with gr.Tab('Account Balance'):
|
| 103 |
+
gr.Markdown('Select only one option. Leave all boxes blank if none of the options fits the client.')
|
| 104 |
+
x1 = gr.Checkbox(1, label='No account')
|
| 105 |
+
x2 = gr.Checkbox(0, label='No balance')
|
| 106 |
+
x3 = gr.Checkbox(0, label='Some balance')
|
| 107 |
+
with gr.Tab('Payment status of previous credit'):
|
| 108 |
+
gr.Markdown('Select only one option. Leave all boxes blank if none of the options fits the client.')
|
| 109 |
+
x4 = gr.Checkbox(1, label='Some problems')
|
| 110 |
+
x5 = gr.Checkbox(0, label='No problems in this bank')
|
| 111 |
+
with gr.Tab('Purpose'):
|
| 112 |
+
gr.Markdown('Select only one option. Leave all boxes blank if none of the options fits the client.')
|
| 113 |
+
x6 = gr.Checkbox(1, label='New car')
|
| 114 |
+
x7 = gr.Checkbox(0, label='Other')
|
| 115 |
+
with gr.Tab('Value savings/stocks'):
|
| 116 |
+
gr.Markdown('Select only one option. Leave all boxes blank if none of the options fits the client.')
|
| 117 |
+
x8 = gr.Checkbox(1, label='No savings')
|
| 118 |
+
x9 = gr.Checkbox(0, label='DM betwenn [100, 1000]')
|
| 119 |
+
x10 = gr.Checkbox(0, label='DM >= 1000')
|
| 120 |
+
with gr.Tab('Length of current employment'):
|
| 121 |
+
gr.Markdown('Select only one option. Leave all boxes blank if none of the options fits the client.')
|
| 122 |
+
x11 = gr.Checkbox(1, label='Below 1 year (or unemployed)')
|
| 123 |
+
x12 = gr.Checkbox(0, label='Between 4 and 7 years')
|
| 124 |
+
with gr.Tab('Instalment per cent'):
|
| 125 |
+
gr.Markdown('Select only one option. Leave all boxes blank if none of the options fits the client.')
|
| 126 |
+
x13 = gr.Checkbox(0, label='Smaller than 20%')
|
| 127 |
+
with gr.Tab('Guarantors'):
|
| 128 |
+
gr.Markdown('Select only one option. Leave all boxes blank if none of the options fits the client.')
|
| 129 |
+
x14 = gr.Checkbox(0, label='No guarantors')
|
| 130 |
+
with gr.Tab('Duration in current address'):
|
| 131 |
+
gr.Markdown('Select only one option. Leave all boxes blank if none of the options fits the client.')
|
| 132 |
+
x15 = gr.Checkbox(1, label='Less than a year')
|
| 133 |
+
x16 = gr.Checkbox(0, label='Between 1 and 4 years')
|
| 134 |
+
with gr.Tab('Most valuable available asset'):
|
| 135 |
+
gr.Markdown('Select only one option. Leave all boxes blank if none of the options fits the client.')
|
| 136 |
+
x17 = gr.Checkbox(1, label='Not available / no assets')
|
| 137 |
+
x18 = gr.Checkbox(0, label='Ownership of house or land')
|
| 138 |
+
with gr.Tab('Concurrent credits'):
|
| 139 |
+
gr.Markdown('Select only one option. Leave all boxes blank if none of the options fits the client.')
|
| 140 |
+
x19 = gr.Checkbox(0, label='No further running credits')
|
| 141 |
+
with gr.Tab('Type of apartment'):
|
| 142 |
+
gr.Markdown('Select only one option. Leave all boxes blank if none of the options fits the client.')
|
| 143 |
+
x20 = gr.Checkbox(0, label='Free apartment')
|
| 144 |
+
with gr.Tab('Number of credits at this Bank'):
|
| 145 |
+
gr.Markdown('Select only one option. Leave all boxes blank if none of the options fits the client.')
|
| 146 |
+
x21 = gr.Checkbox(0, label='One credit')
|
| 147 |
+
with gr.Tab('Occupation'):
|
| 148 |
+
gr.Markdown('Select only one option. Leave all boxes blank if none of the options fits the client.')
|
| 149 |
+
x22 = gr.Checkbox(0, label='Unemployed or unskilled with no permanent')
|
| 150 |
+
predict_button = gr.Button('Predict')
|
| 151 |
+
prediction_output = gr.Label(num_top_classes=2)
|
| 152 |
+
with gr.Accordion('Metrics and plots'):
|
| 153 |
+
with gr.Tab('Metrics'):
|
| 154 |
+
with gr.Row():
|
| 155 |
+
precision_output = gr.Label()
|
| 156 |
+
with gr.Row():
|
| 157 |
+
recall_output = gr.Label()
|
| 158 |
+
with gr.Row():
|
| 159 |
+
f1_output = gr.Label()
|
| 160 |
+
with gr.Tab('Feature Importances'):
|
| 161 |
+
fimp_output = gr.Plot()
|
| 162 |
+
with gr.Tab('Confusion Matrix'):
|
| 163 |
+
cfm_output = gr.Plot()
|
| 164 |
+
predict_button.click(fn=predict_credit_worthiness,
|
| 165 |
+
inputs=[name, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18, x19, x20, x21, x22],
|
| 166 |
+
outputs=[prediction_output,precision_output, recall_output, f1_output, fimp_output, cfm_output])
|
| 167 |
+
gr.Markdown('''
|
| 168 |
+
Want to work in a project together or have interest in my services? Reach me:
|
| 169 |
+
[Linkedin](https://www.linkedin.com/in/marcilioduarte98/)
|
| 170 |
+
[Github](https://github.com/marcilioduarte)
|
| 171 |
+
@marcilioduarte | Economics and Data Science
|
| 172 |
+
''')
|
| 173 |
+
demo.launch()
|