File size: 4,151 Bytes
a824eab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import plotly.express as px
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import RandomizedSearchCV

df = pd.read_csv("credit_risk_dataset.csv")

df = df.dropna()

df.columns

X =df.drop("loan_status", axis = 1)
y = df['loan_status']

categorical_features = ["person_home_ownership", "loan_intent", "loan_grade", "cb_person_default_on_file"]
X = pd.get_dummies(X, categorical_features)
X.columns



from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2)

X_train.head()

from sklearn.preprocessing import StandardScaler
scaler_normal = StandardScaler()
def scaler(data, runtime = False):
    normal_col = ['person_income','person_age','person_emp_length', 'loan_amnt','loan_int_rate','cb_person_cred_hist_length','loan_percent_income']
    if(runtime == False):
        data.loc[:,normal_col] = scaler_normal.fit_transform(data.loc[:,normal_col])
    else:
        data.loc[:,normal_col] = scaler_normal.transform(data.loc[:,normal_col])
    return data

X_train = scaler(X_train)
X_test = scaler(X_test, True)

model = RandomForestClassifier(max_depth = 5)
model.fit(X_train, y_train)

y_predict = model.predict(X_test)

features = {
    "person_home_ownership": ['MORTGAGE', 'OTHER','OWN', 'RENT',],
    "loan_intent": ['DEBTCONSOLIDATION', 'EDUCATION', 'HOMEIMPROVEMENT', 'MEDICAL', 'PERSONAL', 'VENTURE'],
    "loan_grade": ['A','B', 'C', 'D', 'E','F', 'G'],
    "cb_person_default_on_file": ['N', 'Y']
}
def preprocess(model_input):
    for feature in features:
        for option in features[feature]:
            selection = model_input[feature]
            if option is selection:
                model_input[f'{feature}_{option}'] = 1
            else:
                model_input[f'{feature}_{option}'] = 0
                
    model_input.drop([_ for _ in features], inplace = True, axis = 1)
    return model_input

    

def credit_run(person_age, person_income, person_home_ownership,
       person_emp_length, loan_intent, loan_grade, loan_amnt,
       loan_int_rate, cb_person_default_on_file, cb_person_cred_hist_length):
    model_input = preprocess(
    pd.DataFrame( { 'person_age': person_age,
                   'person_income': person_income,
                   'person_home_ownership': person_home_ownership,
                   'person_emp_length': person_emp_length,
                   'loan_intent': loan_intent,
                   'loan_grade': loan_grade,
                   'loan_amnt': loan_amnt,
                   'loan_int_rate': loan_int_rate,
                   'loan_percent_income': loan_amnt / person_income,
                   'cb_person_default_on_file': cb_person_default_on_file,
                   'cb_person_cred_hist_length': cb_person_cred_hist_length
                  }, index = [0]
    ))
    out = model.predict(model_input)
    return "High risk of defaulting" if out[0] == 1 else "Low risk of defaulting" 


import gradio as gr
demo = gr.Interface(
    fn = credit_run,
    inputs = [
        gr.Slider(label="Person_Age(In Years)", minimum=18, maximum=90, step=1),
        gr.Number(label="person_income(per month)"),
        gr.Radio(['MORTGAGE', 'OTHER','OWN', 'RENT']),
        gr.Slider(label="person_emp_length(In Years)", minimum=0, maximum=60, step=1),
        gr.Radio(['DEBTCONSOLIDATION', 'EDUCATION', 'HOMEIMPROVEMENT', 'MEDICAL', 'PERSONAL', 'VENTURE']),
        gr.Radio(['A','B', 'C', 'D', 'E','F', 'G']),
        gr.Number(label="loan_amnt"),
        gr.Number(label="loan_int_rate"),
        gr.Radio(['0', '1']),
        gr.Number(label="cb_person_cred_hist_length"),
             ],
    outputs = "text",
    title = "Credit Risk Predictor",
    examples = [[23,15000,'RENT',2,'EDUCATION','A',300000,8.9,'0',6],
[34,50000,'OWN',1,'MEDICAL','B',1000000,10.65,'0',3],
[42,30000,"MORTGAGE",12,'HOMEIMPROVEMENT','C',800000,7.9,'1',8],
[42,20000,"MORTGAGE",10,'PERSONAL','F',100000,15.25,'1',5]]
)
demo.launch(share = True,debug=True)