-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbankcreditsML1.py
236 lines (95 loc) · 2.47 KB
/
bankcreditsML1.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/env python
# coding: utf-8
# In[2]:
import pandas as pd
import numpy as np
from collections import Counter
# In[3]:
data=pd.read_csv('3_Full_Join_Cleaned.csv')
data.head()
# In[ ]:
# In[4]:
data.columns
# In[5]:
from sklearn.preprocessing import LabelEncoder
ToEncodeVars = ['feature_1','feature_5','feature_11','feature_23','feature_27','feature_32','feature_33',
'feature_36','feature_37','feature_46','feature_48','feature_58',
'feature_59','feature_60','feature_62','feature_72','feature_79']
enc=LabelEncoder()
# In[6]:
for i in ToEncodeVars:
data[[i]] = enc.fit_transform(data[[i]])
# In[7]:
x=data.iloc[:,:-1]
x.head()
# In[8]:
data1 = data.drop('Bad_label',axis=1)
data1.head()
# In[9]:
y=data['Bad_label']
# In[10]:
x1=data1.iloc[:,:-1]
x1.head()
# In[11]:
y.head()
# In[12]:
from sklearn.model_selection import train_test_split
x_train, x_test ,y_train, y_test = train_test_split(x1,y,random_state = 10)
# # Random Forest
# In[13]:
from sklearn.ensemble import RandomForestClassifier
model = RandomForestClassifier(n_estimators = 100,random_state = 20)
# In[14]:
model.fit(x_train,y_train)
# In[15]:
x_train.head()
y_test.head()
# In[16]:
from sklearn.metrics import accuracy_score
y_predict = model.predict(x_test)
print(accuracy_score(y_test,y_predict))
# In[ ]:
# In[17]:
importances = model.feature_importances_
print(importances)
indices = np.argsort(importances)
print(indices)
features = x.columns
print(features)
# In[18]:
from sklearn.metrics import confusion_matrix
# In[19]:
cm = confusion_matrix(y_test,y_predict)
# In[20]:
cm
# In[21]:
#LOGESTIC REGRESSION
# In[22]:
from sklearn.metrics import classification_report
# In[23]:
print(classification_report(y_test,y_predict))
# In[24]:
import matplotlib.pyplot as plt
# In[25]:
from sklearn.linear_model import LogisticRegression
# In[26]:
model = LogisticRegression()
model.fit(x_test,y_test)
# In[27]:
y_predict = model.predict(x_test)
# In[28]:
from sklearn.metrics import accuracy_score
# In[29]:
print(accuracy_score(y_test,y_predict))
# In[37]:
import matplotlib.pyplot as plt
plt.rcParams['figure.figsize'] = 10,20
plt.figure(1)
plt.title('Feature Importance')
plt.barh(range(len(indices)),importances[indices], color = 'b', align = 'center')
plt.yticks(range(len(indices)), features[indices])
plt.xlabel('Relative Importance')
plt.show()
# In[ ]:
# In[ ]:
# In[ ]: