In [2]:
from hmmlearn import hmm
import numpy as np
import json
import pandas as pd
%matplotlib inline

In [2]:
#Takes the JSON files and reloads them back into python
device = "Refrigerator"
def open_device_15min(device_type, device_num):
    with open("Devices/{0}/{0}_{1}.json".format(device_type,device_num)) as f:
        return pd.DataFrame(json.load(f)['time_15'])

fridges = [open_device_15min(device,i)for i in range(8)]

In [3]:
for fridge in fridges:
    print fridge['values']


0       0
1     125
2     105
3       0
4       0
5       0
6       0
7       0
8       0
9      26
10    165
11    148
12    140
13    134
14    130
...
81    133
82    130
83    128
84    127
85    125
86    124
87     31
88      0
89      0
90      0
91      0
92      0
93      0
94    128
95    153
Name: values, Length: 96, dtype: int64
0      0
1     24
2      0
3      0
4     71
5     74
6      8
7      0
8      0
9     79
10    74
11     2
12     0
13     0
14    77
...
81    76
82    20
83     0
84     0
85    37
86    77
87    43
88     0
89     0
90     9
91    79
92    65
93     0
94     0
95     0
Name: values, Length: 96, dtype: int64
0       0
1      58
2       0
3       0
4     117
5       7
6       0
7      20
8     105
9       0
10      0
11     52
12     73
13      0
14      0
...
81     58
82     72
83      0
84      4
85    100
86      0
87      0
88    109
89      3
90      0
91    108
92      0
93      0
94    102
95     15
Name: values, Length: 96, dtype: int64
0      0
1     11
2     16
3      0
4     12
5     16
6      0
7      7
8     20
9      0
10     3
11    24
12     0
13     0
14    28
...
81     0
82    13
83    15
84     0
85     0
86    28
87     0
88     0
89    29
90     0
91     0
92    28
93     0
94     0
95    15
Name: values, Length: 96, dtype: int64
0      0
1     26
2      0
3     25
4      0
5      0
6     23
7      0
8     12
9     13
10     0
11    26
12     0
13     0
14    25
...
81     0
82    30
83     0
84    29
85     0
86     8
87    18
88     0
89    28
90     0
91    24
92     1
93     2
94    23
95     0
Name: values, Length: 96, dtype: int64
0       0
1       1
2       0
3      21
4     104
5       0
6       0
7      28
8     100
9       0
10      0
11     15
12    113
13      0
14      0
...
81     49
82      0
83     44
84     68
85      0
86     29
87     98
88      0
89      0
90    116
91      7
92      0
93      8
94    121
95      0
Name: values, Length: 96, dtype: int64
0      0
1     27
2      0
3      0
4     27
5      0
6      9
7     17
8      0
9     21
10     6
11     0
12    26
13     0
14     0
...
81     0
82    25
83     0
84    22
85     2
86     0
87    25
88     0
89     1
90    26
91     0
92    16
93     8
94     0
95    25
Name: values, Length: 96, dtype: int64
0      0
1     69
2      0
3      0
4     32
5     77
6     53
7      0
8      0
9     41
10    76
11    52
12     0
13     0
14    31
...
81    45
82    76
83    35
84     0
85    22
86    77
87    60
88     0
89     0
90    52
91    76
92    23
93     0
94     0
95    66
Name: values, Length: 96, dtype: int64

In [4]:
# inital starting spots drawn from priors
pi = np.random.dirichlet([1,1])
A = np.array([np.random.dirichlet([1,1]) for i in range(2)])

l = 0,100
r = 10,1
b = 2,2
w = 2,2

mu = [np.random.normal(l[i],r[i]) for i in range(2)]
tao = [np.random.gamma(b[i],w[i]) for i in range(2)]

print "pi: {}".format(pi)
print "A: {}".format(A)
print "mu: {}".format(mu)
print "tao: {}".format(tao)


pi: [ 0.48303771  0.51696229]
A: [[ 0.73626824  0.26373176]
 [ 0.51275998  0.48724002]]
mu: [-7.999801982421592, 100.05137732302529]
tao: [0.7853048679403617, 3.1444401468713794]

In [5]:
startprob = pi
transmat = A
means = mu
covars = tao

ghmm = hmm.GaussianHMM(n_components = 2)

In [6]:
def generate_input(fridge):
    return np.column_stack([map(int,fridge["values"].values > 0),fridge["values"].values])
X = generate_input(fridges[0])
print X[:10]
ghmm.fit([X])


[[  0   0]
 [  1 125]
 [  1 105]
 [  0   0]
 [  0   0]
 [  0   0]
 [  0   0]
 [  0   0]
 [  0   0]
 [  1  26]]
Out[6]:
GaussianHMM(algorithm='viterbi', covariance_type='diag', covars_prior=0.01,
      covars_weight=1,
      init_params='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
      means_prior=None, means_weight=0, n_components=2, n_iter=10,
      params='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
      random_state=None, startprob=None, startprob_prior=1.0, thresh=0.01,
      transmat=None, transmat_prior=1.0)

In [9]:
Ys = [generate_input(fridge) for fridge in fridges]
print Ys[0][:,0]
print
print ghmm.predict(Ys[1])


[0 1 1 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0 0
 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0
 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 1 1]

[1 0 1 1 0 0 0 1 1 0 0 0 1 1 0 0 1 1 1 0 0 0 1 1 0 0 0 1 1 1 0 0 0 1 1 0 0
 0 0 1 1 1 0 0 0 1 1 0 0 0 0 1 1 0 0 0 1 1 0 0 0 1 1 1 0 0 0 1 1 0 0 0 1 1
 1 0 0 1 1 1 0 0 0 1 1 0 0 0 1 1 0 0 0 1 1 1]

In [10]:
[ghmm.score(Y) for Y in Ys]
ghmm.get_params()

dir(ghmm)


Out[10]:
['__class__',
 '__delattr__',
 '__dict__',
 '__doc__',
 '__format__',
 '__getattribute__',
 '__hash__',
 '__init__',
 '__module__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__weakref__',
 '_accumulate_sufficient_statistics',
 '_algorithm',
 '_compute_log_likelihood',
 '_covariance_type',
 '_covars_',
 '_decode_map',
 '_decode_viterbi',
 '_do_backward_pass',
 '_do_forward_pass',
 '_do_mstep',
 '_do_viterbi_pass',
 '_generate_sample_from_state',
 '_get_algorithm',
 '_get_covars',
 '_get_means',
 '_get_param_names',
 '_get_startprob',
 '_get_transmat',
 '_init',
 '_initialize_sufficient_statistics',
 '_log_startprob',
 '_log_transmat',
 '_means_',
 '_set_algorithm',
 '_set_covars',
 '_set_means',
 '_set_startprob',
 '_set_transmat',
 'algorithm',
 'covariance_type',
 'covars_',
 'covars_prior',
 'covars_weight',
 'decode',
 'eval',
 'fit',
 'get_params',
 'init_params',
 'means_',
 'means_prior',
 'means_weight',
 'n_components',
 'n_features',
 'n_iter',
 'params',
 'predict',
 'predict_proba',
 'random_state',
 'sample',
 'score',
 'score_samples',
 'set_params',
 'startprob_',
 'startprob_prior',
 'thresh',
 'transmat_',
 'transmat_prior']

In [11]:
ghmm._get_covars()


Out[11]:
[array([[  1.69491525e-04,   0.00000000e+00],
        [  0.00000000e+00,   9.94477619e+02]]),
 array([[ 0.00027027,  0.        ],
        [ 0.        ,  0.00027027]])]

In [12]:
ghmm._get_means()


Out[12]:
array([[   1.        ,  123.11864407],
       [   0.        ,    0.        ]])

In [13]:
ghmm._get_transmat()


Out[13]:
array([[ 0.89655172,  0.10344828],
       [ 0.18918919,  0.81081081]])

In [14]:
ghmm._get_startprob()


Out[14]:
array([  2.22054605e-16,   1.00000000e+00])

In [15]:
ghmm.decode(X)


Out[15]:
(113.34638769258592,
 array([1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
        1, 1, 0, 0]))

In [ ]: