-
Notifications
You must be signed in to change notification settings - Fork 3
/
main_result_parallel_new.py
147 lines (112 loc) · 4.95 KB
/
main_result_parallel_new.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
# NEED TO RUN ON CLUSTER
import sys
CLUSTER=True
if CLUSTER:
sys.path.insert(0, '/if6/nb2cz/anaconda/lib/python2.7/site-packages')
import numpy as np
import pandas as pd
from create_df import read_df
df, dfc, all_homes, appliance_min, national_average = read_df()
K_min, K_max = 1,6
F_min, F_max=1,8
from all_functions import *
from features import *
import sys
from sklearn.neighbors import KNeighborsRegressor
from sklearn.cross_validation import ShuffleSplit
from sklearn.cross_validation import LeaveOneOut
NUM_NEIGHBOUR_MAX = 6
F_MAX = 6
import json
from sklearn.cross_validation import LeaveOneOut
from sklearn.ensemble import ExtraTreesRegressor
from sklearn.neighbors import KNeighborsRegressor
from collections import OrderedDict
def _find_accuracy(home, appliance, feature="Monthly"):
np.random.seed(42)
appliance_df = df.ix[all_homes[appliance]]
if appliance=="hvac":
start, stop=5, 11
else:
start, stop=1, 13
test_homes = [home]
train_homes = appliance_df[~appliance_df.index.isin([home])].index
all_home_appliance = deepcopy(all_homes)
all_home_appliance[appliance] = train_homes
# Cross validation on inner loop to find best feature, K
train_size = len(train_homes)
l = LeaveOneOut(train_size)
out = OrderedDict()
for cv_train, cv_test in l:
cv_train_home=appliance_df.ix[train_homes[cv_train]]
cv_test_home = appliance_df.ix[train_homes[cv_test]]
test_home_name = cv_test_home.index.values[0]
#print cv_test_home
out[test_home_name]={}
# Summing up energy across start to stop to get Y to learn optimum feature on
Y = cv_train_home[['%s_%d' %(appliance, i) for i in range(start, stop)]].sum(axis=1).values
forest = ExtraTreesRegressor(n_estimators=250,
random_state=0)
forest.fit(cv_train_home[feature_map[feature]], Y)
importances = forest.feature_importances_
indices = np.argsort(importances)[::-1]
# Now varying K and top-N features
for K in range(K_min, K_max):
out[test_home_name][K]={}
for top_n in range(F_min,F_max):
out[test_home_name][K][top_n]=[]
top_n_features = cv_train_home[feature_map[feature]].columns[indices][:top_n]
# Now fitting KNN on this
for month in range(start, stop):
clf = KNeighborsRegressor(n_neighbors=K)
clf.fit(cv_train_home[top_n_features], cv_train_home['%s_%d' %(appliance, month)])
out[test_home_name][K][top_n].append(clf.predict(cv_test_home[top_n_features]))
# Now, finding the (K, top_n) combination that gave us best accuracy on CV test homes
accur = {}
for K in range(K_min, K_max):
accur[K] = {}
for top_n in range(F_min, F_max):
temp = {}
for h in out.iterkeys():
pred = pd.DataFrame(out[h][K][top_n]).T
#all_but_h = [x for x in out.keys() if x!=h]
pred.index = [h]
pred.columns = [['%s_%d' %(appliance, i) for i in range(start, stop)]]
gt = appliance_df.ix[h][['%s_%d' %(appliance, i) for i in range(start, stop)]]
error = (pred-gt).abs().div(gt).mul(100)
mean_error = error.mean().mean()
a = 100-mean_error
if a<0:
a=0
temp[h]=a
ac = pd.Series(temp).mean()
accur[K][top_n] = ac
accur_df = pd.DataFrame(accur)
accur_max = accur_df.max().max()
max_ac_df = accur_df[accur_df==accur_max]
F_best = cv_train_home[feature_map[feature]].columns[indices][:max_ac_df.mean(axis=1).dropna().index.values[0]].tolist()
K_best = max_ac_df.mean().dropna().index.values[0]
# Now predicting for test home
train_overall = appliance_df.ix[appliance_df[~appliance_df.index.isin([home])].index]
test_overall = appliance_df[appliance_df.index.isin([home])]
pred_test = {}
gt_test = {}
for month in range(start, stop):
clf = KNeighborsRegressor(n_neighbors=K_best)
clf.fit(train_overall[F_best], train_overall['%s_%d' %(appliance, month)])
pred_test[month] = clf.predict(test_overall[F_best])
gt_test[month] = test_overall['%s_%d' %(appliance, month)]
json.dump({'f':F_best, 'k':K_best,'accuracy':accur_max},open("../main-out-new/%s_%s_%d.json" %(appliance,feature, home),"w") )
pred_df = pd.DataFrame(pred_test)
pred_df.index = [home]
gt_df = pd.DataFrame(gt_test)
error = (gt_df-pred_df).abs().div(gt_df).mul(100)
accuracy_test = 100-error
accuracy_test[accuracy_test<0]=0
return accuracy_test.squeeze()
import sys
appliance, feature, home = sys.argv[1], sys.argv[2], sys.argv[3]
home = int(home)
out_df = _find_accuracy(home, appliance, feature)
out_df.to_csv("../main-out-new/%s_%s_%d.csv" %(appliance, feature, home))
#_save_csv(out_df, "../main-out", appliance, feature)