-
Notifications
You must be signed in to change notification settings - Fork 1
/
3orderMarkov.py
718 lines (617 loc) · 22.7 KB
/
3orderMarkov.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
#This file considers only "Time of Day"
#Compute P(w4|w3,w2,w1) using 4 gram model
import numpy as np
from Mocapy import *
from numpy import *
from scipy import *
#import statsmodels.api as sm
import pandas
from patsy import dmatrices
from decimal import *
from pandas import *
import pandas as pd
from cluster import KMeansClustering
import numpy as np
from numpy import vstack,array
from scipy.cluster.vq import kmeans,vq
from cluster import KMeansClustering
#from RndCoV import RndCov
#from ghmm import *
from sklearn import hmm
from sklearn.hmm import MultinomialHMM
from itertools import *
import operator
import xlrd
import xlwt
import csv
import time
import re
from datetime import datetime
from pytz import timezone
import pytz
#import xlwt
import pdb
import matplotlib
import matplotlib.pyplot as plt
import pylab
from pylab import plot, show
from matplotlib.dates import date2num
from time import mktime
import matplotlib.pylab as mp
from matplotlib.dates import MinuteLocator, DateFormatter, HourLocator
from pylab import figure
from scipy.optimize import curve_fit
import nltk
from nltk.model import NgramModel
from nltk.probability import GoodTuringProbDist
from nltk.probability import ConditionalFreqDist
from nltk.probability import ConditionalProbDist
from nltk.probability import LidstoneProbDist
import operator
from nltk import *
from itertools import chain
from math import log
from nltk.probability import (ConditionalProbDist, ConditionalFreqDist,
SimpleGoodTuringProbDist)
from nltk.util import ingrams
from nltk.model.api import ModelI
import MyNgram
from MyNgram import MyNgramModel
from MyNgram import *
#read two files
with open('20130128_offices.csv','rb') as csvfile:
offices = csv.reader(csvfile)
officelist = list()
for row in offices:
officelist.append(row)
officelist.pop(0)
OfficeId = list()
for item in officelist:
OfficeId.append(item[0])
Num_Office = len(OfficeId)
print "The number of office is ",
print Num_Office
with open('20130323_waiting_times.csv') as csvfile:
waiting_times = csv.reader(csvfile)
waiting_timeslist = list()
for row in waiting_times:
waiting_timeslist.append(row)
waiting_timeslist.pop(0)
number = len(waiting_timeslist)
#define a hashfunction to map the same bucket (ignoring weekday) to a unique value
#Then put the hash value in item[6]
def Hashfunction(item):
Hash = (str(item[4].weekday())+str(item[4].hour).zfill(2)+str((item[4].minute)/10))
return Hash
#Define a PutInMap function : put the item with the same hash value into the same key
#the key is the has value
#the value is a list
#0:total waiting time 1: total times
def PutInMap(item,Map):
temp = item[6]
if(temp not in Map):
Map[temp] = list()
Map[temp].append(float(item[3])) #if temp is not in hashmap yet
Map[temp].append(1)
else:
Map[temp][0] = Map[temp][0] + float(item[3]) #add the waiting time
Map[temp][1] = Map[temp][1] + 1 #count the times
#This function deals with test data set
#The key is the hash value
#the value is a list
#put the corresponding wo_appointment into the list
def PutInMap2(item,Map):
temp = item[6]
if(temp not in Map):
Map[temp] = list()
Map[temp].append(item[3])
else:
Map[temp].append(item[3])
# Compare the key sets of two dictionary
# If they are equal, return true
def ComKeyDic(dic1,dic2):
return (set(dic1.keys()) == set(dic2.keys()))
# Convert String to Datetime
# each id:
# 0:d 1:office_id 2:w_appointment 3:wo_appointment 4:created_at 5:updated_at 6:Hash Value
col = 4
for i in xrange(number):
#extract the time string and convert them to standard EST datetime
temp = datetime.strptime(waiting_timeslist[i][col], "%Y-%m-%d %H:%M:%S")
utc = pytz.UTC
ams = pytz.timezone('US/Pacific')
waiting_timeslist[i][col] = utc.localize(temp)
waiting_timeslist[i][col] = waiting_timeslist[i][col].astimezone(ams)
temp = waiting_timeslist[i]
waiting_timeslist[i].append(Hashfunction(temp))
#Split the whole set into train_set and test_set
train_set = waiting_timeslist[1:number/2]
test_set = waiting_timeslist[number/2:]
AllTimeList = list()
WaitTimeList = list()
#Office548 = dict()
Officeid = 632
WaitTimeArray = list()
for item in waiting_timeslist:
if(int(item[1])==Officeid):
if(item[4].weekday()==2):
if(item[4].hour>=9 and item[4].hour<=17):
if(item[4].hour!=17):
WaitTimeArray.append(float(item[3]))
AllTimeList.append(item[6])
else:
if(item[4].minute==0):
WaitTimeArray.append(float(item[3]))
AllTimeList.append(item[6])
else:
if(item[4].hour>=8 and item[4].hour<=17):
if(item[4].hour!=17):
WaitTimeArray.append(float(item[3]))
AllTimeList.append(item[6])
else:
if(item[4].minute==0):
WaitTimeArray.append(float(item[3]))
AllTimeList.append(item[6])
#Remove the day with all zeros
def FindRemoveDay(TempTime):
Day = dict()
for item in TempTime:
if (item[0] not in Day):
Day[item[0]] = 1
else:
TempCount = Day[item[0]] + 1
Day[item[0]] = TempCount
return max(Day.iterkeys() , key=(lambda key : Day[key]))
groups = []
uniquekeys = []
for k, g in groupby(enumerate(zip(AllTimeList,WaitTimeArray)), lambda(i,(x,y)): y==0.0):
groups.append(list(g))
uniquekeys.append(k)
RemoveIndex = list()
for i in range(len(uniquekeys)):
if(uniquekeys[i]==True):
if(len(groups[i])>=50):
(Index,TempValue) = zip(*groups[i])
(TempTime,TempWaitTime) = zip(*TempValue)
RemoveDay = FindRemoveDay(TempTime)
for j in range(len(TempTime)):
if(TempTime[j][0]==RemoveDay):
RemoveIndex.append(Index[j])
tempWaitTime = [item for i, item in enumerate(WaitTimeArray) if i not in RemoveIndex]
WaitTimeArray = tempWaitTime
tempTime = [item for i , item in enumerate(AllTimeList) if i not in RemoveIndex]
AllTimeList = tempTime
'''
#Compute the centroids for this office
WaitTimeArray = list()
for item in waiting_timeslist:
if(int(item[1])==Officeid):
WaitTimeArray.append(float(item[3]))
AllTimeList.append(item[6])
'''
'''
WaitTimeArray = np.array(WaitTimeArray)
np.vstack(WaitTimeArray)
###############################################################change the number of clusters here
centroids,_ = kmeans(WaitTimeArray,5, iter=30,thresh=1e-6)
clusters,_ = vq(WaitTimeArray,centroids)
print "The centroids are: " + repr(centroids)
print "The result of clustering is :" + repr(clusters)
NumCluster = len(centroids)
print "The number of centroids is :" + repr(NumCluster)
#Construct the training data
TimeList = list()
for i in xrange(len(WaitTimeArray)/2):
TimeList.append(AllTimeList[i])
WaitTimeList.append(clusters[i])
#Construct the testing data
TestTimeList = list()
TestWaitTimeList = list()
for i in xrange(len(WaitTimeArray)/2,len(WaitTimeArray)):
TestTimeList.append(AllTimeList[i])
TestWaitTimeList.append(clusters[i])
'''
#################################################################equi-height histogram
NumElement = len(WaitTimeArray)
SortedWaitTime = sorted(WaitTimeArray)
'''
ZeroWaitTime = list()
NonZeroWaitTime = list()
for i in range(len(SortedWaitTime)):
if(SortedWaitTime[i]==0):
ZeroWaitTime.append(0)
else:
NonZeroWaitTime.append(SortedWaitTime[i])
'''
###############################################change the number of group here
NumGroup = 20
#NumNonZero = len(SortedWaitTime)
Count = math.ceil(float(NumElement)/NumGroup)
GroupWaitTime = dict()
GroupIndex = 0
GroupWaitTime[GroupIndex] = list()
for i in range(NumElement):
GroupWaitTime[GroupIndex].append(SortedWaitTime[i])
if((i+1) % Count == 0):
GroupIndex = GroupIndex + 1
GroupWaitTime[GroupIndex] = list()
centroids= list()
#centroids.append(0.0) #add zeros to centroids
for key in GroupWaitTime:
centroids.append(sum(GroupWaitTime[key])/float(len(GroupWaitTime[key])))
centroids = sorted(centroids)
# change the continous waiting time into discrete waiting time
DisWaitingTime = list()
for i in range(len(WaitTimeArray)):
'''
if(WaitTimeArray[i]==0):
DisWaitingTime.append(WaitTimeArray[i])
'''
DisWaitingTime.append(min(centroids,key=lambda x:abs(x-WaitTimeArray[i])))
centroids = sorted(centroids)
# draw the histogram for all the data for each bucket
TotalMap = dict()
for i in range(len(WaitTimeArray)):
if(TotalMap.has_key(AllTimeList[i])):
TotalMap[AllTimeList[i]].append(WaitTimeArray[i])
else:
TotalMap[AllTimeList[i]] = list()
TotalMap[AllTimeList[i]].append(WaitTimeArray[i])
'''
keyList = list()
for key in sorted(TotalMap.iterkeys()):
keyList.append(key)
for k in range(1,13):
plt.figure(k)
j = 1
for i in range((k-1)*22,(k-1)*22+22):
plt.subplot(4,6,j)
TempList = list(TotalMap[keyList[i]])
plt.hist(TempList)
#plt.title(keyList[i])
plt.savefig(str(k) + '.jpg')
j = j + 1
#plt.autoscale_view(True,True,True)
plt.show()
'''
'''
TotalMapList = list()
#for
#frequency, bin_edges = np.histogram(np.array(TotalMap[key]))
#plt.subplot(12,22,i)
#TempList = list(TotalMap[key])
#plt.hist(TempList)
#plt.title(key)
#i = i + 1
#TotalMapList.extend(list(frequency))
plt.hist(TotalMapList)
plt.show()
'''
#draw the histogram for all the data for each hour
'''
HourMap = dict()
for i in range(len(WaitTimeArray)):
Hour = AllTimeList[i][0] + AllTimeList[i][1] + AllTimeList[i][2]
if(HourMap.has_key(Hour)):
HourMap[Hour].append(WaitTimeArray[i])
else:
HourMap[Hour] = list()
HourMap[Hour].append(WaitTimeArray[i])
HourKeyList = list()
for key in sorted(HourMap.iterkeys()):
HourKeyList.append(key)
'''
#draw the histogram for the training data for each hour
TrainWaitTimeArray = WaitTimeArray[:len(WaitTimeArray)/2]
TrainTimeList = AllTimeList[:len(AllTimeList)/2]
HourMap = dict()
for i in range(len(TrainWaitTimeArray)):
Hour = TrainTimeList[i][0] + TrainTimeList[i][1] + TrainTimeList[i][2]
if(HourMap.has_key(Hour)):
HourMap[Hour].append(TrainWaitTimeArray[i])
else:
HourMap[Hour] = list()
HourMap[Hour].append(TrainWaitTimeArray[i])
HourKeyList = list()
for key in sorted(HourMap.iterkeys()):
HourKeyList.append(key)
#Define model function to be used to fit to the data
def gauss(x, *p):
A, mu, sigma = p
return A*np.exp(-(x-mu)**2/(2.*sigma**2))
GaussianMap = dict()
for key in HourKeyList:
data = HourMap[key]
hist, bin_edges = np.histogram(data,density=True)
bin_centres = (bin_edges[:-1] + bin_edges[1:])/2
#p0 = [1.,0.,1.]
x = sum(bin_centres*hist)/sum(hist)
#coeff, var_matrix = curve_fit(gauss, bin_centres, hist,p0=p0)
#hist_fit = gauss(bin_centres, *coeff)
width = sqrt(abs(sum((bin_centres-x)**2*hist)/sum(hist)))
GaussianMap[key] = list()
GaussianMap[key].append(x)
GaussianMap[key].append(width)
#fit Gaussian distribution to each hour's waiting time
'''
for k in range(13,24):
plt.figure(k)
j = 1
for i in range((k-13)*4, (k-12)*4):
plt.subplot(2,2,j)
TempList = list(HourMap[HourKeyList[i]])
plt.hist(TempList)
plt.title(str(int(HourKeyList[i][0])+1) + HourKeyList[i][1] + HourKeyList[i][2])
plt.savefig('Hour'+str(k) + '.jpg')
j = j + 1
plt.show()
'''
######################################Convert the discrete waiting time into the index of state number, DisWaitingTime
for i in range(len(WaitTimeArray)):
temp = centroids.index(DisWaitingTime[i])
DisWaitingTime[i] = temp
#Construct the training data, WaitTimeList and TimeList is training data
TimeList = list()
for i in xrange(len(WaitTimeArray)/2):
TimeList.append(AllTimeList[i])
WaitTimeList.append(DisWaitingTime[i])
#Construct the testing data(discrete waiting time)
TestTimeList = list()
TestWaitTimeList = list()
for i in xrange(len(WaitTimeArray)/2,len(WaitTimeArray)):
TestTimeList.append(AllTimeList[i])
TestWaitTimeList.append(DisWaitingTime[i])
DisWaitingTime = np.array(DisWaitingTime)
WaitingTimeValue = np.unique(DisWaitingTime)
NumWaitingTime = WaitingTimeValue.size
#convert the time value to integer and discard "Weekday"
TimeList = [(x[1]+x[2]+x[3]) for x in TimeList]
TimeList = [int(x) for x in TimeList]
TimeArray = np.array(TimeList)
TimeValue = np.unique(TimeArray)
NumTime = len(TimeValue)
#Calculate the probability P(wt|time)
#Convert timevalue to string
TimeString = [str(x).zfill(3) for x in TimeList]
#TimeString = [(str(int(x[0])+1) + x[1:]) for x in TimeString]
TimeValueString = [str(x).zfill(3) for x in list(TimeValue)]
#TimeValueString = [(str(int(x[0])+1) + x[1:]) for x in TimeValueString]
data = np.zeros((NumTime,NumWaitingTime),'d')
PredictWaiting = DataFrame(data,index=TimeValueString,columns=WaitingTimeValue)
for i in range(len(WaitTimeList)):
tempindex = TimeString[i]
tempwaitingtime = WaitTimeList[i]
count = PredictWaiting.get_value(tempindex,tempwaitingtime)
count = count + 1
PredictWaiting.set_value(tempindex,tempwaitingtime,count)
for row in TimeValueString:
SumRow = sum(PredictWaiting.ix[row])
for column in WaitingTimeValue:
Current = PredictWaiting.get_value(row,column)
Current = (float(Current)+1)/(SumRow+len(WaitingTimeValue))
PredictWaiting.set_value(row,column,Current)
writer = ExcelWriter('BaseLine.xls')
#PredictWaiting.to_excel(writer,sheet_name='sheet1')
data = zeros(NumTime,'d')
BasePre = Series(data,index=TimeValueString)
for row in TimeValueString:
MaxValue = (PredictWaiting.ix[row]).argmax()
BasePre[row] = MaxValue
BasePre.to_csv('BaseLine')
#################################################Calculate the errors using P(wt|h)
#discard the weekday in the testing data
TestTimeList = [(x[1]+x[2]+x[3]) for x in TestTimeList]
#TestTimeList = [int(x) for x in TestTimeList]
totalCount = 0
totalError = 0
for i in range(len(TestTimeList)):
totalCount = totalCount + 1
totalError = totalError + abs(centroids[TestWaitTimeList[i]]-centroids[int(BasePre[TestTimeList[i]])])
errorRate = float(totalError)/totalCount
#errorRate is 0.5235 hour
#Construct the training sequences
index = 0
WaitingTimeSeq = dict()
TimeSeq = dict()
OriginalWaitingTimeSeq = dict()
key = 0
while(index<len(TimeList)):
WaitingTimeSeq[key] = list()
TimeSeq[key] = list()
OriginalWaitingTimeSeq[key] = list()
print key
while (True):
if((index+2)<=len(TimeList) and TimeList[index+1] >= TimeList[index]):
WaitingTimeSeq[key].append(DisWaitingTime[index])
TimeSeq[key].append(TimeList[index])
OriginalWaitingTimeSeq[key].append(WaitTimeArray[index])
index = index + 1
print index
continue
else:
WaitingTimeSeq[key].append(DisWaitingTime[index])
TimeSeq[key].append(TimeList[index])
OriginalWaitingTimeSeq[key].append(WaitTimeArray[index])
key = key + 1
index = index + 1
break
NumSeq = key
#index of minimum waiting_time
Index0 = np.argmin(centroids)
###############construct the input for nltk model
f = open('WaitingTimeSeq','w')
#ssw = list()
for key in TimeSeq:
#tempString = list()
for i in range((len(TimeSeq[key]))):
f.write(str(WaitingTimeSeq[key][i])+" ")
f.write('\n')
#est = lambda freqdist,bins: LidstoneProbDist(freqdist,0.2)
#ngramModel = NgramModel(4,ssw,False,False)
##############construct the reverse sequence
f = open('reverseWaitingTimeSeq','w')
for key in TimeSeq:
index = range(len(TimeSeq[key]))
index.reverse()
for i in index:
f.write(str(WaitingTimeSeq[key][i])+" ")
f.write('\n')
#read in the 4GramLM ARPA file
ConditionalMap = dict()
BackWeightMap = dict()
f1 = open('4GramLM','r')
for line in f1:
tempString = line.split('\t')
if(len(tempString)>=2):
Ngram = tempString[1].rstrip('\n')
ConditionalMap[Ngram] = 10**float(tempString[0].rstrip('\n'))
if(len(tempString)>=3):
BackWeightMap[Ngram] = 10**float(tempString[2])
#Construct testing sequence
key = 0
index = 0
TestWaitTimeSeq = dict()
TestTimeSeq = dict()
#TestOriginWaitTimeSeq = dict()
while(index<len(TestTimeList)):
TestWaitTimeSeq[key] = list()
TestTimeSeq[key] = list()
print key
while (True):
if((index+2)<=len(TestTimeList) and TestTimeList[index+1] >= TestTimeList[index]):
TestTimeSeq[key].append(TestTimeList[index])
TestWaitTimeSeq[key].append(TestWaitTimeList[index])
index = index + 1
print index
continue
else:
TestWaitTimeSeq[key].append(TestWaitTimeList[index])
TestTimeSeq[key].append(TestTimeList[index])
# TestOriginWaitTimeSeq[key].append(TestOriginWaitTimeList[index])
#TestWeekSeq[key].append(TestWeekList[index])
# TestWeekTimeSeq[key].append(TestWeekTimeList[index])
key = key + 1
index = index + 1
break
#################Set Lambda
Lambda = 0.3
totalError = 0.0
totalCount = 0
for key in TestTimeSeq:
for i in range(3,len(TestTimeSeq[key])):
totalCount = totalCount + 1
Prediction = dict()
for j in WaitingTimeValue:
tempString = str(TestWaitTimeSeq[key][i-3])+" "+str(TestWaitTimeSeq[key][i-2])+" "+str(TestWaitTimeSeq[key][i-1])+" "+str(j)
prob = 1
alpha = 1
while(ConditionalMap.has_key(tempString)==False):
tempString1 = " ".join(tempString.split(" ")[1:])
tempString2 = " ".join(tempString.split(" ")[:-1])
if(ConditionalMap.has_key(tempString2)):
alpha = alpha * BackWeightMap[tempString2]
else:
alpha = 1 * alpha
tempString = tempString1
prob = alpha * ConditionalMap[tempString]
finalprob = Lambda * math.log(prob) + (1-Lambda) * math.log(PredictWaiting.get_value(TestTimeSeq[key][i],j))
Prediction[j] = finalprob
predictWaiting = max(Prediction.iteritems(), key=operator.itemgetter(1))[0]
totalError = totalError + abs(centroids[predictWaiting]-centroids[TestWaitTimeSeq[key][i]])
print str(totalError/totalCount)
#0.1674 hour (Lambda = 0.5) and 0.1651 hour (Lambda = 0.8)
#0.2032 hour (Lambda = 0.3)
#EMission Matrix
Emission = np.zeros((NumWaitingTime,NumTime),'d')
index = WaitingTimeValue
columns = TimeValue
Emission = DataFrame(Emission,index=index,columns=columns)
Count1 = Series(np.zeros(NumWaitingTime), index=WaitingTimeValue)
for key in WaitingTimeSeq:
for i in xrange(len(WaitingTimeSeq[key])):
Current = Emission.get_value(WaitingTimeSeq[key][i],TimeSeq[key][i])
Current = Current + 1
Count1[WaitingTimeSeq[key][i]] = Count1[WaitingTimeSeq[key][i]] + 1
Emission.set_value(WaitingTimeSeq[key][i],TimeSeq[key][i],Current)
for row in WaitingTimeValue:
for column in TimeValue:
Current = Emission.get_value(row,column)
Current = (Current+1)/(Count1[row]+NumTime)
Emission.set_value(row,column,Current)
#Initial Probability
Initial = Series(np.zeros(NumWaitingTime), index = WaitingTimeValue)
for key in WaitingTimeSeq:
if (key>0) :
Current = Initial[WaitingTimeSeq[key][0]]
Current = Current + 1
Initial[WaitingTimeSeq[key][0]] = Current
Initial = (Initial+1)/(NumSeq-1+NumWaitingTime)
#Convert pandas dataframe to numpy array
subset = Transition[WaitingTimeValue]
tuples = [tuple(x) for x in subset.values]
Transition = np.array(tuples)
subset = Emission[TimeValue]
tuples = [tuple(x) for x in subset.values]
Emission = np.array(tuples)
Initial = np.array(Initial)
#Use HMM to predict the waiting time
n_components = NumWaitingTime
n_symbols = NumTime
startprob = Initial
transmat = Transition
emissionprob_ = Emission
emitter = hmm.MultinomialHMM(n_components, startprob, transmat)
emitter.emissionprob_ = emissionprob_
observations = arange(NumTime)
observations = observations.reshape(len(observations),)
Prediction = emitter.predict(observations)
FinalPrediction = list()
#"Prediction" is the discrete waiting time state prediction
#"FinalPrediction" is the continous waiting time prediction
for i in xrange(len(Prediction)):
FinalPrediction.append(centroids[Prediction[i]])
print "The prediction is: " + repr(Prediction)
print "The FinalPrediction[i]" + repr(FinalPrediction)
plt.plot(FinalPrediction)
plt.ylabel('Waiting Hours')
plt.xlabel('Time(Week+Day)')
FinalPrediction = Series(FinalPrediction,index=TimeValue)
###############################################################################change between continous and discrete waiting time here
#convert the discrete waiting time in testing data to continous value
for i in xrange(len(TestWaitTimeList)):
TestWaitTimeList[i] = centroids[TestWaitTimeList[i]]
#TestMap is the dictionary whose key is "Time", and value is "Waiting time list"
TestMap = dict()
for i in xrange(len(TestTimeList)):
if(TestMap.has_key(int(TestTimeList[i]))):
TestMap[int(TestTimeList[i])].append(TestWaitTimeList[i])
else:
TestMap[int(TestTimeList[i])] = list()
TestMap[int(TestTimeList[i])].append(TestWaitTimeList[i])
Prediction = Series(Prediction,index=TimeValue)
#Compute the percentage of prediction error using "discrete waiting time state"
ErrorCount = 0
for key in Prediction.index:
PredictionState = Prediction[key]
for value in TestMap[key]:
if(value!=PredictionState):
ErrorCount = ErrorCount+1
TotalCount = len(TestTimeList)
ErrorPercent = float(ErrorCount)/TotalCount
print "The percentage of error is: " + repr(ErrorPercent)
#Compute the average prediction error using "continous waiting time"
DifferMap = {}
for key in FinalPrediction.index:
a = map(float, TestMap[key])
b = [FinalPrediction[key]]*len(TestMap[key])
DifferList = [a-b for a,b in zip(a,b)]
Average_Error = sum(map(abs,DifferList))/len(DifferList)
DifferMap[key] = list()
#DifferMap[key].append(CountTime[key][2])
DifferMap[key].append(Average_Error)
error = (sum(DifferMap.values()))/float(len(DifferMap))
print "The average error is: " + repr(error) +" hours"
#plt.title('No Decrease, Error is: '+ repr(error)+ '(hour)')
#plt.title('Decrease by 5/6, Error is: '+repr(error) + '(hour)')
plt.title('3 State Error Percentage: '+ repr(ErrorPercent))
plt.show()