-
Notifications
You must be signed in to change notification settings - Fork 0
/
mlp_reg.py
262 lines (198 loc) · 8.67 KB
/
mlp_reg.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import pandas as pd
import csv
import numpy as np
from sklearn.neighbors import KNeighborsClassifier
from sklearn.neighbors import KNeighborsRegressor
from sklearn.neural_network import MLPClassifier
from sklearn.neural_network import MLPRegressor
from geopy.distance import vincenty
from pyproj import Proj
from math import radians, cos, sin, asin, sqrt
from sklearn.model_selection import GridSearchCV
from sklearn.svm import SVR
from sklearn.preprocessing import Imputer
from sklearn.ensemble import RandomForestRegressor
def haversine(lon1, lat1, lon2, lat2):
"""
Calculate the great circle distance between two points
on the earth (specified in decimal degrees)
"""
# convert decimal degrees to radians
lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
# haversine formula
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
c = 2 * asin(sqrt(a))
r = 6371 # Radius of earth in kilometers. Use 3956 for miles
return c * r *1000 #return in meters
#---------------------------------------------------------------------------------------------------------------
def regression_allset(Y_test_lon,Y_test_lat,X_test,ml_lon,ml_lat): #Only for tests
#Turn into list
predicts_lon = ml_lon.predict(X_test)
predicts_lat = ml_lat.predict(X_test)
error = []
for j in range(len(X_test)):
#change the latitude and longitude unit
myProj = Proj("+proj=utm +zone=23K, +south +ellps=WGS84 +datum=WGS84 +units=m +no_defs")
lon_pred,lat_pred = myProj(predicts_lon[j], predicts_lat[j], inverse=True)
lon_Y, lat_Y = myProj(Y_test_lon[j], Y_test_lat[j], inverse=True)
#join in a unique list
Y = []
Y.append(lon_Y)
Y.append(lat_Y)
predict = []
predict.append(lon_pred)
predict.append(lat_pred)
#The distance between the two latitudes is the error
distance = vincenty(Y, predict).meters
#If you want to use haversine distance, uncomment the line below
# distance = haversine(lon_Y, lat_Y, lon_pred, lat_pred)
error.append(distance)
return np.mean(error),predicts_lon,predicts_lat
#--------------------------------------------------------------------------------------------------------------
#Calculate how many measurements each cell phone has
def show_number_measurements(grouped_df):
for i in range(len(grouped_df)):
print "Measures:" + str(len(grouped_df[i][1])) + ", PHONEID" + str((grouped_df[i][1]).PHONEID.unique())
print "\n"
#---------------------------------------------------------------------------------------------------------------
#Create a list of data frames. Each smartphone has its own data frame
def create_phone_df(df,grouped_df):
list_phones = df.PHONEID.unique()
df_phone = []
j=0
for i in range(0,24):
if (i in list_phones):
df_phone.append(grouped_df[j][1])
j=j+1
else:
df_phone.append([])
return df_phone, list_phones
#---------------------------------------------------------------------------------------------------------------
def undersampling(df_phone, phones_used):
minimum = 10000000
und_df_phone = []
for i in phones_used:
#find the smaller data frame
if(len(df_phone[i]) < minimum):
minimum = len(df_phone[i])
ind_min = i
#unsampling the others data frames so they are the same size
for i in phones_used:
if(i != ind_min):
und_df_phone.append(df_phone[i].sample(n=minimum))
else:
und_df_phone.append(df_phone[i])
return und_df_phone
#---------------------------------------------------------------------------------------------------------------
def shuffle(und_df_phone):
for i in range(len(und_df_phone)):
und_df_phone[i] = und_df_phone[i].sample(frac=1)
return und_df_phone
#---------------------------------------------------------------------------------------------------------------
def init_list_of_objects(size):
list_of_objects = list()
for i in range(0,size):
list_of_objects.append( list() ) #different object reference each time
return list_of_objects
#---------------------------------------------------------------------------------------------------------------
#return the number of hits
def compare(Y_test_build, predictions_build, Y_test_floor, predictions_floor):
hits = 0
#if tests and predictions have the same number of building and the same number of floor, the algorithm hit
for i in range(len(Y_test_floor)):
if(Y_test_build[i] == predictions_build[i] and Y_test_floor[i] == predictions_floor[i]):
hits = hits +1
return hits
#---------------------------------------------------------------------------------------------------------------
def save_vec(hit_rate_build_mlp,hit_rate_floor_mlp,hit_rate_build_knn, hit_rate_floor_knn):
np.save("build_mlp.npy",hit_rate_build_mlp)
np.save("floor_mlp.npy",hit_rate_floor_mlp)
np.save("build_knn.npy",hit_rate_build_knn)
np.save("floor_knn.npy",hit_rate_floor_knn)
#---------------------------------------------------------------------------------------------------------------
def load_vec():
hit_rate_build_mlp = np.load("build_mlp.npy")
hit_rate_floor_mlp = np.load("floor_mlp.npy")
hit_rate_build_knn = np.load("build_knn.npy")
hit_rate_floor_knn = np.load("floor_knn.npy")
#---------------------------------------------------------------------------------------------------------------
def KFold(k, und_df_phone):
#und_df_phone = shuffle(und_df_phone)
phone = []
#split the data frame of each smartphone
for j in range(len(und_df_phone)):
phone.append(np.array_split(und_df_phone[j],k)) #the first dimension of "phone" is each phone, the second is the splits data frames from that smatphone
"""
model = MLPRegressor(learning_rate = 'adaptive')
solvers = ['lbfgs', 'sgd', 'adam']
activations = ['identity', 'logistic', 'tanh']
max_its = [200,400,600]
mlp_lon = GridSearchCV(estimator=model, param_grid=dict(solver = solvers,activation =activations,max_iter=max_its),n_jobs=4) #GRID
mlp_lat = GridSearchCV(estimator=model, param_grid=dict(solver = solvers,activation =activations,max_iter=max_its),n_jobs=4) #GRID
"""
lon = RandomForestRegressor()
lat = RandomForestRegressor()
#creating a empty list with size len(und_df_phone)
mean_error = init_list_of_objects(len(und_df_phone))
list_pred_lon = init_list_of_objects(len(und_df_phone))
list_pred_lat = init_list_of_objects(len(und_df_phone))
for i in range(k):
#separate each smartphone's data frame in test and train
test = [] #list of data frames
train =pd.DataFrame()
for j in range(len(und_df_phone)):
test.append(phone[j][i])
#Join the train set
for x in range(k):
if x != i:
train = pd.concat([train,phone[j][x]])
#Training with total training set
X_train = train.ix[:,0:519].fillna(0).as_matrix().astype(float)
Y_train_lon = train['LONGITUDE'].fillna(0).as_matrix().astype(float)
Y_train_lat = train['LATITUDE'].fillna(0).as_matrix().astype(float)
lon.fit(X_train,Y_train_lon)
lat.fit(X_train,Y_train_lat)
#test all phones
for j in range(len(und_df_phone)):
#only pick up from test set the phone that you will be evaluated
data_test = test[j].ix[:,0:519].fillna(0).as_matrix().astype(float)
Y_test_lon = test[j]['LONGITUDE'].fillna(0).as_matrix().astype(float)
Y_test_lat = test[j]['LATITUDE'].fillna(0).as_matrix().astype(float)
error, pred_lon, pred_lat = regression_allset(Y_test_lon,Y_test_lat,data_test,mlp_lon,mlp_lat)
mean_error[j].append( error )
list_pred_lon[j].append(pred_lon)
list_pred_lat[j].append(pred_lat)
#save data
np.save("pred_lon.npy", list_pred_lon)
np.save("pred_lat.npy", list_pred_lat)
np.save("mean_error_RandomForest.npy", mean_error_mlp)
print "mean error regression RandomForest"
print str(np.mean(mean_error[0])) + " - " + str(np.std(mean_error[0]))
print str(np.mean(mean_error[1])) + " - " + str(np.std(mean_error[1]))
print str(np.mean(mean_error[2])) + " - " + str(np.std(mean_error[2]))
print str(np.mean(mean_error[3])) + " - " + str(np.std(mean_error[3]))
print " "
print "Best Params Lon"
print lon.best_params_
print "Best Params Lat"
print lat.best_params_
#---------------------------------------------------------------------------------------------------------------
def main():
#defines
phones_used = [6,7,13,14]
k=10
#convert csv file in an data frame
df = pd.read_csv('trainingData.csv')
#group by pohneID
grouped_df = list(df.groupby(['PHONEID']))
#show_number_measurements(grouped_df)
#create a data frame for each phone
df_phone, list_phones = create_phone_df(df,grouped_df)
#Doing undersampling
und_df_phone = undersampling(df_phone,phones_used)
KFold(k, und_df_phone)
if __name__ == "__main__":
main()
#to do a list of data frames. Each smartphone has its own data frame