-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuserRegression.py
174 lines (125 loc) · 5.35 KB
/
userRegression.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
from sklearn.linear_model import LinearRegression, Lasso, Ridge
import justTry
import displayRegression
def fitprederr(classifier, xtrain, ytrain, xtest, ytest):
classifier.fit(xtrain, ytrain)
predictions = classifier.predict(xtest)
sqerrors = [(test - pred)**2 for test, pred in zip(ytest, predictions)]
sqerror = sum(sqerrors)/float(len(sqerrors))
abserrors = [abs(test - pred) for test, pred in zip(ytest, predictions)]
abserror = sum(abserrors)/float(len(abserrors))
return (sqerror, abserror)
users, businesses = justTry.crossUserReviewsBus(minrev=10,maxrev=20)
linearsq = []
linearabs = []
lassosq = []
lassoabs = []
ridgesq = []
ridgeabs = []
frac1 = 9
frac2 = 10
for i, user in zip(range(0, len(users.keys())), users.keys()):
ratedbusinesses = users[user]
split = frac1*(len(ratedbusinesses.values())/frac2)
y = ratedbusinesses.values()[:split]
x = [businesses[bus_id] for bus_id in ratedbusinesses.keys()][:split]
cvlinearsq = []
cvlinearabs = []
cvlassosq = []
cvlassoabs = []
cvridgesq = []
cvridgeabs = []
for j in range(0, frac1):
xtrain = [x[k] for k in range(0, len(x)) if k%frac1!=j]
xtest = [x[k] for k in range(0, len(x)) if k%frac1==j]
ytrain = [y[k] for k in range(0, len(y)) if k%frac1!=j]
ytest = [y[k] for k in range(0, len(y)) if k%frac1==j]
lreg = LinearRegression()
lasso = Lasso()
ridge = Ridge()
cvlinsqerror, cvlinabserror = fitprederr(lreg, xtrain, ytrain, xtest, ytest)
cvlassqerror, cvlasabserror = fitprederr(lasso, xtrain, ytrain, xtest, ytest)
cvridsqerror, cvridabserror = fitprederr(ridge, xtrain, ytrain, xtest, ytest)
cvlinearsq.append(cvlinsqerror)
cvlinearabs.append(cvlinabserror)
cvlassosq.append(cvlassqerror)
cvlassoabs.append(cvlasabserror)
cvridgesq.append(cvridsqerror)
cvridgeabs.append(cvridabserror)
linsqerror = sum(cvlinearsq)/float(len(cvlinearsq))
linabserror = sum(cvlinearabs)/float(len(cvlinearabs))
lassqerror = sum(cvlassosq)/float(len(cvlassosq))
lasabserror = sum(cvlassoabs)/float(len(cvlassoabs))
ridsqerror = sum(cvridgesq)/float(len(cvridgesq))
ridabserror = sum(cvridgeabs)/float(len(cvridgeabs))
linearsq.append(linsqerror)
linearabs.append(linabserror)
lassosq.append(lassqerror)
lassoabs.append(lasabserror)
ridgesq.append(ridsqerror)
ridgeabs.append(ridabserror)
linsqerror = sum(linearsq)/float(len(linearsq))
linabserror = sum(linearabs)/float(len(linearabs))
print "CV: Avg Linear:", linsqerror, linabserror
lassqerror = sum(lassosq)/float(len(lassosq))
lasabserror = sum(lassoabs)/float(len(lassoabs))
print "CV: Avg Lasso:", lassqerror, lasabserror
ridsqerror = sum(ridgesq)/float(len(ridgesq))
ridabserror = sum(ridgeabs)/float(len(ridgeabs))
print "CV: Avg Ridge:", ridsqerror, ridabserror
displayRegression.displayRegression(linearsq, ridgesq, lassosq)
displayRegression.displayRegressionInOrder(linearsq, ridgesq, lassosq,users)
linearsq = []
linearabs = []
lassosq = []
lassoabs = []
ridgesq = []
ridgeabs = []
dumbsq = []
dumbabs = []
for i, user in zip(range(0, len(users.keys())), users.keys()):
ratedbusinesses = users[user]
y = ratedbusinesses.values()
x = [businesses[bus_id] for bus_id in ratedbusinesses.keys()]
split = frac1*(len(x)/frac2)
xtrain = x[:split]
xtest = x[split:]
ytrain = y[:split]
ytest = y[split:]
lreg = LinearRegression()
lasso = Lasso()
ridge = Ridge()
linsqerror, linabserror = fitprederr(lreg, xtrain, ytrain, xtest, ytest)
linearsq.append(linsqerror)
linearabs.append(linabserror)
lassqerror, lasabserror = fitprederr(lasso, xtrain, ytrain, xtest, ytest)
lassosq.append(lassqerror)
lassoabs.append(lasabserror)
ridsqerror, ridabserror = fitprederr(ridge, xtrain, ytrain, xtest, ytest)
ridgesq.append(ridsqerror)
ridgeabs.append(ridabserror)
dumbpred = [features[0] for features in x]
dumbsqerrors = [(test - pred)**2 for test, pred in zip(ytest, dumbpred)]
dumbsqerror = sum(dumbsqerrors)/float(len(dumbsqerrors))
dumbabserrors = [abs(test - pred) for test, pred in zip(ytest, dumbpred)]
dumbabserror = sum(dumbabserrors)/float(len(dumbabserrors))
dumbsq.append(dumbsqerror)
dumbabs.append(dumbabserror)
linsqerror = sum(linearsq)/float(len(linearsq))
linabserror = sum(linearabs)/float(len(linearabs))
print "Test: Avg Linear:", linsqerror, linabserror
lassqerror = sum(lassosq)/float(len(lassosq))
lasabserror = sum(lassoabs)/float(len(lassoabs))
print "Test: Avg Lasso:", lassqerror, lasabserror
ridsqerror = sum(ridgesq)/float(len(ridgesq))
ridabserror = sum(ridgeabs)/float(len(ridgeabs))
print "Test: Avg Ridge:", ridsqerror, ridabserror
dumbsqerror = sum(dumbsq)/float(len(dumbsq))
dumbabserror = sum(dumbabs)/float(len(dumbabs))
print "Test: Avg Dumb:", dumbsqerror, dumbabserror
bigger = [1 for i, ridge in enumerate(ridgesq) if ridge <= dumbsq[i]]
smaller = [ridge - dumbsq[i] for i, ridge in enumerate(ridgesq) if ridge > dumbsq[i]]
print "Better success in: ", sum(bigger), " which is ", (float(sum(bigger))/len(ridgesq)), " of the values"
print smaller
displayRegression.displayRegressionCompare1(ridgesq, dumbsq)
displayRegression.displayRegressionCompare(ridgesq, dumbsq, users)