-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassifier.py
120 lines (73 loc) · 2.53 KB
/
classifier.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
from sklearn import tree
from sklearn import ensemble
from sklearn import linear_model
from sklearn.metrics import accuracy_score
modelPack = {}
def trees( x_train, x_test, y_train, y_test ):
res = []
print("hello trees")
m = tree.DecisionTreeClassifier()
m.fit(x_train, y_train)
print("fiting")
predictions = m.predict(x_test)
acc = accuracy_score(y_test,predictions)
modelPack['DecisionTreeClassifier'] = m
res.append( ( acc , "DecisionTreeClassifier" ) )
m = tree.ExtraTreeClassifier()
m.fit(x_train, y_train)
predictions = m.predict(x_test)
acc = accuracy_score(y_test,predictions)
modelPack['ExtraTreeClassifier'] = m
res.append( ( acc , "ExtraTreeClassifier" ) )
print(res)
return res
def ensembles( x_train, x_test, y_train, y_test ):
res = []
m = ensemble.AdaBoostClassifier()
m.fit(x_train, y_train)
predictions = m.predict(x_test)
acc = accuracy_score(y_test,predictions)
modelPack['AdaBoostClassifier'] = m
res.append( ( acc , "AdaBoostClassifier" ) )
# print(res)
m = ensemble.BaggingClassifier()
m.fit(x_train, y_train)
predictions = m.predict(x_test)
acc = accuracy_score(y_test,predictions)
modelPack['BaggingClassifier'] = m
res.append( ( acc , "BaggingClassifier" ) )
m = ensemble.GradientBoostingClassifier()
m.fit(x_train, y_train)
predictions = m.predict(x_test)
acc = accuracy_score(y_test,predictions)
modelPack['GradientBoostingClassifier'] = m
res.append( ( acc , "GradientBoostingClassifier" ) )
return res
def lines( x_train, x_test, y_train, y_test ):
res = []
m = linear_model.RidgeClassifier()
m.fit(x_train, y_train)
predictions = m.predict(x_test)
acc = accuracy_score(y_test,predictions)
modelPack['RidgeClassifier'] = m
res.append( ( acc , "RidgeClassifier" ) )
m = linear_model.SGDClassifier()
m.fit(x_train, y_train)
predictions = m.predict(x_test)
acc = accuracy_score(y_test,predictions)
modelPack['SGDClassifier'] = m
res.append( ( acc , "SGDClassifier" ) )
return res
def classify( x_train, x_test, y_train, y_test ):
result = {}
r1 = trees( x_train, x_test, y_train, y_test )
r2 = lines( x_train, x_test, y_train, y_test )
r3 = ensembles( x_train, x_test, y_train, y_test )
res = r1 + r2 + r3
res.sort(reverse=True)
print(res)
models = {}
for val , name in res[:4]:
result[name] = val
models[name] = modelPack[name]
return result , models