-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClassification.py
191 lines (146 loc) · 6.14 KB
/
Classification.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
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import sys
import datetime
from sklearn.neural_network import MLPClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn import svm
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import GaussianNB
from sklearn.naive_bayes import ComplementNB
from sklearn.model_selection import train_test_split
from sklearn.metrics import plot_confusion_matrix
from sklearn.metrics import classification_report
from collections import Counter
#logit test
def logit_test(target, features, num):
y=target
X = features
train_X, test_X, train_y, test_y = train_test_split(X, y, test_size=0.20, random_state=0)
clf = LogisticRegression()
print('logit has begun')
print(datetime.datetime.now(),'\n')
clf.fit(train_X,train_y)
print('fitted')
print(datetime.datetime.now(),'\n')
yp=clf.predict(test_X)
print('predicted')
print(datetime.datetime.now(),'\n')
print(clf.coef_)
print('Predicted == Real: ', clf.score(test_X,test_y),'\n')
#confusion matrix
titles_options = [("Confusion matrix", None), ("Confusion matrix, normalized", 'true')]
for title, normalize in titles_options:
disp = plot_confusion_matrix(clf, test_X, test_y, cmap=plt.cm.Blues, normalize=normalize)
disp.ax_.set_title(title)
plt.savefig('C:/Users/Saulo/source/repos/Classification-Project/Figures/Classification/'+title+'_LR'+num+'.png')
plt.close()
#classification report
print('Classification Report: \n\n',classification_report(test_y, yp),
'\n____________________________________________________________\n')
#logit test end
#knei test
def kn(target, features, num):
y=target
X = features
train_X, test_X, train_y, test_y = train_test_split(X, y, test_size=0.20, random_state=0)
clf = KNeighborsClassifier()
print('knei has begun')
print(datetime.datetime.now(),'\n')
clf.fit(train_X,train_y)
print('fitted')
print(datetime.datetime.now(),'\n')
yp=clf.predict(test_X)
print('predicted')
print(datetime.datetime.now(),'\n')
print('Predicted == Real: ', clf.score(test_X,test_y),'\n')
#confusion matrix
titles_options = [("Confusion matrix", None), ("Confusion matrix, normalized", 'true')]
for title, normalize in titles_options:
disp = plot_confusion_matrix(clf, test_X, test_y, cmap=plt.cm.Blues, normalize=normalize)
disp.ax_.set_title(title)
plt.savefig('C:/Users/Saulo/source/repos/Classification-Project/Figures/Classification/'+title+'_KN'+num+'.png')
plt.close()
#classification report
print('Classification Report: \n\n',classification_report(test_y, yp),
'\n____________________________________________________________\n')
#knei test end
#gaussian nb test
def gnb(target, features, num):
y=target
X = features
train_X, test_X, train_y, test_y = train_test_split(X, y, test_size=0.20, random_state=0)
clf = GaussianNB()
print('gaussian nb has begun')
print(datetime.datetime.now(),'\n')
clf.fit(train_X,train_y)
print('fitted')
print(datetime.datetime.now(),'\n')
yp=clf.predict(test_X)
print('predicted')
print(datetime.datetime.now(),'\n')
print('Predicted == Real: ', clf.score(test_X,test_y),'\n')
#confusion matrix
titles_options = [("Confusion matrix", None), ("Confusion matrix, normalized", 'true')]
for title, normalize in titles_options:
disp = plot_confusion_matrix(clf, test_X, test_y, cmap=plt.cm.Blues, normalize=normalize)
disp.ax_.set_title(title)
plt.savefig('C:/Users/Saulo/source/repos/Classification-Project/Figures/Classification/'+title+'_GNB'+num+'.png')
plt.close()
#classification report
print('Classification Report: \n\n',classification_report(test_y, yp),
'\n____________________________________________________________\n')
#gaussian nb test end
#complement nb test
def cnb(target, features, num):
y=target
X = features
train_X, test_X, train_y, test_y = train_test_split(X, y, test_size=0.20, random_state=0)
clf = ComplementNB()
print('complement nb has begun')
print(datetime.datetime.now(),'\n')
clf.fit(train_X,train_y)
print('fitted')
print(datetime.datetime.now(),'\n')
yp=clf.predict(test_X)
print('predicted')
print(datetime.datetime.now(),'\n')
print('Predicted == Real: ', clf.score(test_X,test_y),'\n')
#confusion matrix
titles_options = [("Confusion matrix", None), ("Confusion matrix, normalized", 'true')]
for title, normalize in titles_options:
disp = plot_confusion_matrix(clf, test_X, test_y, cmap=plt.cm.Blues, normalize=normalize)
disp.ax_.set_title(title)
plt.savefig('C:/Users/Saulo/source/repos/Classification-Project/Figures/Classification/'+title+'_CNB'+num+'.png')
plt.close()
#classification report
print('Classification Report: \n\n',classification_report(test_y, yp),
'\n____________________________________________________________\n')
#complement nb test end
#open txt
sys.stdout=open('C:/Users/Saulo/source/repos/Classification-Project/classification_output.txt','w')
data = pd.read_csv("encoded_loan.csv")
target = data.MIS_Status
#1
features = data.drop(columns=['MIS_Status'])
logit_test(target, features, '1')
kn(target,features,'1')
gnb(target,features,'1')
cnb(target,features,'1')
#2
features = features.drop(columns=['CreateJob','RetainedJob'])
logit_test(target, features, '2')
kn(target,features,'2')
gnb(target,features,'2')
cnb(target,features,'2')
#3
features = features.drop(columns=['NewExist'])
logit_test(target, features, '3')
kn(target,features,'3')
gnb(target,features,'3')
cnb(target,features,'3')
#close txt
sys.stdout.close()
#State, NAICS, Term, NoEmp, NewExist, CreateJob, RetainedJob, isFranchise, UrbanRural, LowDoc, GrAppv
#CreateJob and RetainedJob have very little impact according to logit, NewExist looks low as well