-
Notifications
You must be signed in to change notification settings - Fork 0
/
OccupancyDetector.py
400 lines (266 loc) · 10.5 KB
/
OccupancyDetector.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# coding: utf-8
# In[1]:
import pandas as pd, numpy as np
from sklearn.cross_validation import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
from pandas import read_csv
from matplotlib import pyplot
from pandas import concat
from sklearn.metrics import accuracy_score,confusion_matrix,classification_report,roc_auc_score,roc_curve
from sklearn.naive_bayes import GaussianNB
import matplotlib.pyplot as plt
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from pandas.plotting import scatter_matrix
import pickle
# In[2]:
# load all data
data1 = read_csv('datatest.txt', header=0, index_col=1, parse_dates=True, squeeze=True)
data2 = read_csv('datatraining.txt', header=0, index_col=1, parse_dates=True, squeeze=True)
data3 = read_csv('datatest2.txt', header=0, index_col=1, parse_dates=True, squeeze=True)
# In[3]:
# vertically stack and maintain temporal order
data = concat([data1, data2, data3])
# save aggregated dataset
data.to_csv('combined.csv')
# load the dataset
data = read_csv('combined.csv')
# print(data)
# print(data.shape)
# In[4]:
data.head()
# In[5]:
# drop the feature sno and date since it is just index of the data.No dependence with final result.
data.drop(['sno','date'], axis=1, inplace=True)
data.head()
# In[6]:
print('DATA VISUALIZATION')
# In[7]:
data.corr()
# In[8]:
#features humidityRatio and humidity has hight correlation.
# high correlation
print('Correlation Matrix')
plt.figure(figsize=(8,8))
plt.matshow(data.corr(),fignum=1)
plt.xticks(range(len(data.columns)), data.columns)
plt.yticks(range(len(data.columns)), data.columns)
plt.colorbar()
plt.show()
# In[9]:
#Pair-wise Relationships between attributes
print('Pair-wise Relationships')
scatter_matrix(data,figsize=(10,10))
# In[10]:
#Features 'HumidityRatio' and 'Humidity' are fully linear dependent.Therefore drop one of the feature.
data.drop(['HumidityRatio'], axis=1, inplace=True)
data.head()
# In[11]:
print('Accuracy Graph of Simple Logistic Regression Model on each environment measure in isolation')
values = data.values
features = [0, 1, 2, 3]
for f in features:
# split data into inputs and outputs
X1, y1 = values[:, f].reshape((len(values), 1)), values[:, -1]
# split the dataset
train_X, test_X, train_y, test_y = train_test_split(X1, y1, test_size=0.3, random_state=1)
# define the model
model = LogisticRegression()
# fit the model on the training set
model.fit(train_X, train_y)
# predict the test set
y_hat = model.predict(test_X)
# evaluate model skill
score = accuracy_score(test_y, y_hat)
plt.hlines(y=data.columns[f], xmin=0, xmax=score*100)
# In[12]:
#We can see that only the “Light” feature is required in order to achieve 99% accuracy
#Very likely that the office rooms in which the environmental variables
#were recorded had a light sensor that turned internal lights on
#when the room was occupied and off otherwise.
#We decided to remove this feature assuming there is no light sensor in room to generalize the model.
data.drop(['Light'], axis=1,inplace=True)
data.head()
# In[13]:
values = data.values
# split data into inputs and outputs
X, y = values[:, :-1], values[:, -1]
# split the dataset
trainX, testX, trainy, testy = train_test_split(X, y, test_size=0.3, random_state=1)
# In[14]:
def LogisticReg():
# Dump the "TRAINED Logistic Regression" classifier with Pickle
logistic_reg_pkl_filename = 'logistic_reg_classifier.pkl'
# Open the file to save as pkl file
# logistic_reg_model_pkl = open(logistic_reg_pkl_filename, 'wb')
# model = LogisticRegression()
# fit the model on the training set
# model.fit(trainX, trainy)
#dump model
# pickle.dump(model,logistic_reg_model_pkl)
# Loading the saved naive bayes model pickle
logistic_reg_model_pkl = open(logistic_reg_pkl_filename, 'rb')
model = pickle.load(logistic_reg_model_pkl)
# predict the test set
predy = model.predict(testX)
#save output in csv file
logreg_out =pd.DataFrame({'OccupancyLogReg': predy})
# evaluate model skil
print('Accuracy of logistic regression classifier on test set: {:.4f}\n'.format(accuracy_score(testy,predy)))
print('Confusion matrix:')
print(confusion_matrix(testy,predy))
print('\nReport:')
print(classification_report(testy,predy))
print('ROC_Curve')
logit_roc_auc = roc_auc_score(testy, model.predict(testX))
fpr, tpr, thresholds = roc_curve(testy, model.predict_proba(testX)[:,1])
plt.figure()
plt.plot(fpr, tpr, label='Logistic Regression (area = %0.2f)' % logit_roc_auc)
plt.plot([0, 1], [0, 1],'r--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver operating characteristic')
plt.legend(loc="lower right")
plt.savefig('Log_ROC')
plt.show()
# Close the pickle instances
logistic_reg_model_pkl.close()
return logreg_out
# In[15]:
logreg_out=LogisticReg()
# In[16]:
def NaiveBayes():
# Dump the "TRAINED NAIVE BAYES" classifier with Pickle
naive_bayes_pkl_filename = 'naive_bayes_classifier.pkl'
# Open the file to save as pkl file
# naive_bayes_model_pkl = open(naive_bayes_pkl_filename, 'wb')
# model = GaussianNB()
# model.fit(trainX,trainy)
# Dump model
# pickle.dump(model, naive_bayes_model_pkl)
# Loading the saved naive bayes model pickle
naive_bayes_model_pkl = open(naive_bayes_pkl_filename, 'rb')
model = pickle.load(naive_bayes_model_pkl)
#predict Occupancy
predy= model.predict(testX)
#save output in a file
gaussian_out =pd.DataFrame({'OccupancyNaiveBayes': predy})
print('Accuracy of Naive Bayes classifier on test set: {:.4f}\n'.format(accuracy_score(testy,predy)))
print('Confusion matrix:')
print(confusion_matrix(testy,predy))
print('\nReport:')
print(classification_report(testy,predy))
print('ROC_Curve')
logit_roc_auc = roc_auc_score(testy, model.predict(testX))
fpr, tpr, thresholds = roc_curve(testy, model.predict_proba(testX)[:,1])
plt.figure()
plt.plot(fpr, tpr, label='Naive Bayes (area = %0.3f)' % logit_roc_auc)
plt.plot([0, 1], [0, 1],'r--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver operating characteristic')
plt.legend(loc="lower right")
plt.savefig('Log_ROC')
plt.show()
# Close the pickle instances
naive_bayes_model_pkl.close()
return gaussian_out
# In[17]:
gaussian_out=NaiveBayes()
# In[18]:
def DecisionTree():
# Dump the "TRAINED DECISION TREE" classifier with Pickle
decision_tree_pkl_filename = 'decision_tree_classifier.pkl'
# Open the file to save as pkl file
# decision_tree_model_pkl = open(decision_tree_pkl_filename, 'wb')
# tree = DecisionTreeClassifier()
# tree.fit(trainX, trainy)
# pickle.dump(tree, decision_tree_model_pkl)
# Loading the saved decision tree model pickle
decision_tree_model_pkl = open(decision_tree_pkl_filename, 'rb')
model = pickle.load(decision_tree_model_pkl)
#predict occupancy
predy = model.predict(testX)
# save output in csv file
dectree_out =pd.DataFrame({'Occ_DecisionTree': predy})
print('Accuracy of Decision Tree classifier on test set: {:.4f}\n'.format(accuracy_score(testy,predy)))
from sklearn.metrics import classification_report, confusion_matrix
print('Confusion matrix:')
print(confusion_matrix(testy, predy))
print('\nReport:')
print(classification_report(testy, predy))
print('ROC_Curve')
logit_roc_auc = roc_auc_score(testy, model.predict(testX))
fpr, tpr, thresholds = roc_curve(testy, model.predict_proba(testX)[:,1])
plt.figure()
plt.plot(fpr, tpr, label='Decision Tree (area = %0.2f)' % logit_roc_auc)
plt.plot([0, 1], [0, 1],'r--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver operating characteristic')
plt.legend(loc="lower right")
plt.savefig('Log_ROC')
plt.show()
# Close the pickle instances
decision_tree_model_pkl.close()
return dectree_out
# In[19]:
dectree_out=DecisionTree()
# In[20]:
def RandomForest():
# Dump the "TRAINED RANDOM FOREST" classifier with Pickle
random_forest_pkl_filename = 'random_forest_classifier.pkl'
# Open the file to save as pkl file
# random_forest_model_pkl = open(random_forest_pkl_filename, 'wb')
# Create the model with 1500 trees
# model = RandomForestClassifier(n_estimators=1500,
# bootstrap = True,
# max_features = 'sqrt')
# Fit on training data
# model.fit(trainX, trainy)
# Dump model
# pickle.dump(model, random_forest_model_pkl)
# Loading the saved random forest model pickle
random_forest_model_pkl = open(random_forest_pkl_filename, 'rb')
model= pickle.load(random_forest_model_pkl)
# predict occupancy
rf_predictions = model.predict(testX)
# save output in csv file
RandomForest_out =pd.DataFrame({'Occ_RandomForest': rf_predictions})
# predict occupancy probabilities
rf_probs = model.predict_proba(testX)[:, 1]
print('Accuracy of Random Forest classifier on test set: {:.4f}\n'.format(accuracy_score(testy, rf_predictions)))
print('Confusion matrix:')
print(confusion_matrix(testy, rf_predictions))
print('\nReport:')
print(classification_report(testy, rf_predictions))
print('ROC_Curve')
logit_roc_auc = roc_auc_score(testy, model.predict(testX))
fpr, tpr, thresholds = roc_curve(testy, model.predict_proba(testX)[:,1])
pyplot.figure()
pyplot.plot(fpr, tpr, label='Random Forest (area = %0.2f)' % logit_roc_auc)
pyplot.plot([0, 1], [0, 1],'r--')
pyplot.xlim([0.0, 1.0])
pyplot.ylim([0.0, 1.05])
pyplot.xlabel('False Positive Rate')
pyplot.ylabel('True Positive Rate')
pyplot.title('Receiver operating characteristic')
pyplot.legend(loc="lower right")
pyplot.savefig('Log_ROC')
pyplot.show()
# Close the pickle instances
random_forest_model_pkl.close()
return RandomForest_out
# In[21]:
RandomForest_out=RandomForest()
# In[22]:
submission = pd.concat([logreg_out,gaussian_out,dectree_out,RandomForest_out ], axis=1)
submission.to_csv('output2.csv', index=False)
# In[ ]: