-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregressor.py
More file actions
137 lines (81 loc) · 2.75 KB
/
regressor.py
File metadata and controls
137 lines (81 loc) · 2.75 KB
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
from sklearn import tree
import numpy as np
from sklearn import ensemble
from sklearn import linear_model
from sklearn.svm import SVR
from sklearn.metrics import accuracy_score,mean_squared_error
modelPack = {}
def trees( x_train, x_test, y_train, y_test ):
res = []
m = tree.DecisionTreeRegressor()
m.fit(x_train, y_train)
predictions = m.predict(x_test)
acc = mean_squared_error(y_test,predictions)
modelPack['DecisionTreeRegressor'] = m
res.append( ( acc , "DecisionTreeRegressor" ) )
m = tree.ExtraTreeRegressor()
m.fit(x_train, y_train)
predictions = m.predict(x_test)
acc = mean_squared_error(y_test,predictions)
modelPack['ExtraTreeRegressor'] = m
res.append( ( acc , "ExtraTreeRegressor" ) )
print(res)
return res
def ensembles( x_train, x_test, y_train, y_test ):
res = []
m = SVR(kernel='rbf', epsilon=.1)
m.fit(x_train, y_train)
predictions = m.predict(x_test)
acc = mean_squared_error(y_test,predictions)
modelPack['SVM-RBF'] = m
res.append( ( acc , "SVM-RBF" ) )
print("done RBF")
m = SVR(kernel='poly', epsilon=.1)
m.fit(x_train, y_train)
predictions = m.predict(x_test)
acc = mean_squared_error(y_test,predictions)
modelPack["SVM-POLY"] = m
res.append( ( acc , "SVM-POLY" ) )
print("done POLY")
return res
def lines( x_train, x_test, y_train, y_test ):
res = []
m = linear_model.Ridge(alpha=1.0)
m.fit(x_train, y_train)
predictions = m.predict(x_test)
acc =mean_squared_error(y_test,predictions)
modelPack["Ridge"] = m
res.append( ( acc , "Ridge" ) )
m = linear_model.LinearRegression()
m.fit(x_train, y_train)
predictions = m.predict(x_test)
print("preds",predictions)
acc =mean_squared_error(y_test,predictions)
modelPack["Linear Regression"] = m
res.append( ( acc , "Linear Regression" ) )
m = linear_model.Lasso(alpha=0.1)
m.fit(x_train, y_train)
predictions = m.predict(x_test)
acc = mean_squared_error(y_test,predictions)
modelPack["Lasso"] = m
res.append( ( acc , "Lasso" ) )
m = linear_model.LassoLars(alpha=0.1)
m.fit(x_train, y_train)
predictions = m.predict(x_test)
acc = mean_squared_error(y_test,predictions)
modelPack["LassoLARs"] = m
res.append( ( acc , "LassoLARs" ) )
return res
def regression( 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
res.sort()
print(res)
models = {}
for val , name in res[:4]:
result[name] = val
models[name] = modelPack[name]
return result , models