harish199 commited on
Commit
a824eab
1 Parent(s): 3e02132
Files changed (1) hide show
  1. app.py +112 -0
app.py ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import pandas as pd
3
+ import matplotlib.pyplot as plt
4
+ import seaborn as sns
5
+ import plotly.express as px
6
+ from sklearn.tree import DecisionTreeClassifier
7
+ from sklearn.ensemble import RandomForestClassifier
8
+ from sklearn.model_selection import RandomizedSearchCV
9
+
10
+ df = pd.read_csv("credit_risk_dataset.csv")
11
+
12
+ df = df.dropna()
13
+
14
+ df.columns
15
+
16
+ X =df.drop("loan_status", axis = 1)
17
+ y = df['loan_status']
18
+
19
+ categorical_features = ["person_home_ownership", "loan_intent", "loan_grade", "cb_person_default_on_file"]
20
+ X = pd.get_dummies(X, categorical_features)
21
+ X.columns
22
+
23
+
24
+
25
+ from sklearn.model_selection import train_test_split
26
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2)
27
+
28
+ X_train.head()
29
+
30
+ from sklearn.preprocessing import StandardScaler
31
+ scaler_normal = StandardScaler()
32
+ def scaler(data, runtime = False):
33
+ normal_col = ['person_income','person_age','person_emp_length', 'loan_amnt','loan_int_rate','cb_person_cred_hist_length','loan_percent_income']
34
+ if(runtime == False):
35
+ data.loc[:,normal_col] = scaler_normal.fit_transform(data.loc[:,normal_col])
36
+ else:
37
+ data.loc[:,normal_col] = scaler_normal.transform(data.loc[:,normal_col])
38
+ return data
39
+
40
+ X_train = scaler(X_train)
41
+ X_test = scaler(X_test, True)
42
+
43
+ model = RandomForestClassifier(max_depth = 5)
44
+ model.fit(X_train, y_train)
45
+
46
+ y_predict = model.predict(X_test)
47
+
48
+ features = {
49
+ "person_home_ownership": ['MORTGAGE', 'OTHER','OWN', 'RENT',],
50
+ "loan_intent": ['DEBTCONSOLIDATION', 'EDUCATION', 'HOMEIMPROVEMENT', 'MEDICAL', 'PERSONAL', 'VENTURE'],
51
+ "loan_grade": ['A','B', 'C', 'D', 'E','F', 'G'],
52
+ "cb_person_default_on_file": ['N', 'Y']
53
+ }
54
+ def preprocess(model_input):
55
+ for feature in features:
56
+ for option in features[feature]:
57
+ selection = model_input[feature]
58
+ if option is selection:
59
+ model_input[f'{feature}_{option}'] = 1
60
+ else:
61
+ model_input[f'{feature}_{option}'] = 0
62
+
63
+ model_input.drop([_ for _ in features], inplace = True, axis = 1)
64
+ return model_input
65
+
66
+
67
+
68
+ def credit_run(person_age, person_income, person_home_ownership,
69
+ person_emp_length, loan_intent, loan_grade, loan_amnt,
70
+ loan_int_rate, cb_person_default_on_file, cb_person_cred_hist_length):
71
+ model_input = preprocess(
72
+ pd.DataFrame( { 'person_age': person_age,
73
+ 'person_income': person_income,
74
+ 'person_home_ownership': person_home_ownership,
75
+ 'person_emp_length': person_emp_length,
76
+ 'loan_intent': loan_intent,
77
+ 'loan_grade': loan_grade,
78
+ 'loan_amnt': loan_amnt,
79
+ 'loan_int_rate': loan_int_rate,
80
+ 'loan_percent_income': loan_amnt / person_income,
81
+ 'cb_person_default_on_file': cb_person_default_on_file,
82
+ 'cb_person_cred_hist_length': cb_person_cred_hist_length
83
+ }, index = [0]
84
+ ))
85
+ out = model.predict(model_input)
86
+ return "High risk of defaulting" if out[0] == 1 else "Low risk of defaulting"
87
+
88
+
89
+ import gradio as gr
90
+ demo = gr.Interface(
91
+ fn = credit_run,
92
+ inputs = [
93
+ gr.Slider(label="Person_Age(In Years)", minimum=18, maximum=90, step=1),
94
+ gr.Number(label="person_income(per month)"),
95
+ gr.Radio(['MORTGAGE', 'OTHER','OWN', 'RENT']),
96
+ gr.Slider(label="person_emp_length(In Years)", minimum=0, maximum=60, step=1),
97
+ gr.Radio(['DEBTCONSOLIDATION', 'EDUCATION', 'HOMEIMPROVEMENT', 'MEDICAL', 'PERSONAL', 'VENTURE']),
98
+ gr.Radio(['A','B', 'C', 'D', 'E','F', 'G']),
99
+ gr.Number(label="loan_amnt"),
100
+ gr.Number(label="loan_int_rate"),
101
+ gr.Radio(['0', '1']),
102
+ gr.Number(label="cb_person_cred_hist_length"),
103
+ ],
104
+ outputs = "text",
105
+ title = "Credit Risk Predictor",
106
+ examples = [[23,15000,'RENT',2,'EDUCATION','A',300000,8.9,'0',6],
107
+ [34,50000,'OWN',1,'MEDICAL','B',1000000,10.65,'0',3],
108
+ [42,30000,"MORTGAGE",12,'HOMEIMPROVEMENT','C',800000,7.9,'1',8],
109
+ [42,20000,"MORTGAGE",10,'PERSONAL','F',100000,15.25,'1',5]]
110
+ )
111
+ demo.launch(share = True,debug=True)
112
+