-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinearWeightedRegression.py
132 lines (102 loc) · 3.99 KB
/
LinearWeightedRegression.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
import numpy as np
import scipy as sp
import random as r
import code
import math
def linearWeighted(X,Y,xStar,p):
weight = weightMatrix(xStar,X,p);
XWeighted = weight*X;
YWeighted = weight*Y;
print XWeighted;
w = sp.stats.linregress(XWeighted,YWeighted);
return xStar*w[0] + w[1];
def nearestNeighbors(X,Y,xStar,k):
dist = distMatrix(xStar,X);
wOrdering = np.argsort(dist);
sum = 0;
for i in range(0,k):
sum += Y[wOrdering[i]];
return sum/k;
def nearestNeighbors2(X,Y,xStar,k):
dist = distMatrix2(xStar,X);
wOrdering = np.argsort(dist);
sum = 0;
for i in range(0,k):
sum += Y[wOrdering[i]];
return sum/k;
def weightMatrix(xStar,X,p):
weight = np.empty([len(X),1]);
for i in range(0,len(weight)):
val = math.exp(-dist(X[i],xStar)/(p**2));
print val;
weight[i,0] = val;
print weight;
return weight;
def distMatrix(xStar,X):
distance = np.empty([len(X),1]);
for i in range(0,len(distance)):
distance[i,0] = dist(X[i],xStar);
return distance;
def distMatrix2(xStar,X):
distance = np.empty([len(X),1]);
for i in range(0,len(distance)):
distance[i,0] = dist2(X[i],xStar);
return distance;
def main():
xFull2 = np.loadtxt("/projects/onebusaway/BakerNiedMLProject/data/routefeatures/intercitytransit_route13_dist_days_time_dayOfWeek_normalized.txt", dtype=np.float);
yFull2 = np.loadtxt("/projects/onebusaway/BakerNiedMLProject/data/routefeatures/intercitytransit_route13_dev.txt", dtype=np.float);
sel = np.random.permutation(range(len(xFull2)));
split = len(xFull2)/4;
xTrain2 = xFull2[sel[:split*2]];
xVal2 = xFull2[sel[split*2:3*split]];
xTest2 = xFull2[sel[3*split:]];
yTrain2 = yFull2[sel[:split*2]];
yVal2 = yFull2[sel[split*2:3*split]];
yTest2 = yFull2[sel[3*split:]];
#KNN with more features
yHat2 = np.zeros(len(yTest2));
for i in range(0,len(xTest2)):
if i%100 == 0 and i<>0:
print str((100.0*i)/len(xTest2)) + "%";
yHat2[i] = nearestNeighbors2(xTrain2,yTrain2,xTest2[i],100);
np.savetxt("/projects/onebusaway/BakerNiedMLProject/data/modelPredictions/intercitytransit_route13_dev_predicted100NN_normalized_dist_days_time_dayOfWeek.txt",yHat2);
print "rmse = "+str(rmse(yTest2,yHat2));
xFull = np.loadtxt("/projects/onebusaway/BakerNiedMLProject/data/routefeatures/intercitytransit_route13_dist.txt", dtype=np.float);
yFull = np.loadtxt("/projects/onebusaway/BakerNiedMLProject/data/routefeatures/intercitytransit_route13_dev.txt", dtype=np.float);
sel = np.random.permutation(range(len(xFull)));
split = len(xFull)/4;
xTrain = xFull[sel[:split*2]];
xVal = xFull[sel[split*2:3*split]];
xTest = xFull[sel[3*split:]];
yTrain = yFull[sel[:split*2]];
yVal = yFull[sel[split*2:3*split]];
yTest = yFull[sel[3*split:]];
#knn
yHat = np.zeros(len(yTest));
for i in range(0,len(xTest)):
if i%100 == 0 and i<>0:
print str((100.0*i)/len(xTest)) + "%";
yHat[i] = nearestNeighbors(xTrain,yTrain,xTest[i],100);
np.savetxt("/projects/onebusaway/BakerNiedMLProject/data/modelPredictions/intercitytransit_route13_dev_predicted100NN_dist.txt",yHat);
print "rmse = "+str(rmse(yTest,yHat));
#locally weighted linear regression
yHat = np.zeros(len(yTest));
for i in range(0,len(xTest)):
if i%100 == 0:
print "100 done";
yHat[i] = linearWeighted(xTrain, yTrain, xTest[i],1000);
print "rmse = "+str(rmse(yTest,yHat));
def rmse(y,yhat):
yhat = y-yhat;
count = 0;
for i in range(0,n):
count += yhat[i]*yhat[i];
return count/len(yhat);
def dist(x, xStar):
return (x-xStar)**2
def dist2(x, xStar):
return 3*(x[0]-xStar[0])**2 + .5*(x[1]-xStar[1])**2 + 2*(x[2]-xStar[2])**2 + (x[3]-xStar[3])**2;
def dist3(x, xStar):
return (1.0/100.0)*(x[0]-xStar[0])**2 + (1.0)*(x[1]-xStar[1])**2 + (1.0/3600.0)*(x[2]-xStar[2])**2 + (1.0/2.0)*(x[3]-xStar[3])**2;
if __name__ == "__main__":
main()