Generate a pre-defined GaussianHMM model


In [ ]:
import random
import numpy as np
import matplotlib.pyplot as plt

from hmmlearn import hmm


startprob = np.array([1.0, 0.0, 0.0])
transmat = np.array([[0.0, 1.0, 0.0],
                     [0.0, 0.0, 1.0],
                     [0.1, 0.0, 0.0]])
means = np.array([[0.0, 0.0],
                  [5.0, 0.0],
                  [0.0, 5.0]])
covars = [[[ 1.,  0.], [ 0.,  1.]],
          [[ 1.,  0.], [ 0.,  1.]],
          [[ 1.,  0.], [ 0.,  1.]],
          [[ 1.,  0.], [ 0.,  1.]]]

model = hmm.GaussianHMM(n_components=3, covariance_type="full")
model.startprob_ = startprob
model.transmat_ = transmat
model.means_ = means
model.covars_ = covars

In [ ]:
X, Z = model.sample(100)

In [ ]:
print(X[:10])
print(Z[:10])

In [ ]:
plt.plot(X[:, 0], X[:, 1], ".-", label="observations", ms=6, mfc="orange", alpha=0.7)
for i, m in enumerate(means):
    plt.text(m[0], m[1], 'Component %i' % i, size=17, horizontalalignment='center', bbox=dict(alpha=.7, facecolor='w'))
plt.legend(loc='best')
plt.show()

In [ ]:
print(model)
print(model.n_components)
print(model.transmat_)
print(model.startprob_)
print(model.means_)
print(model.covars_)

Use generated samples to train a new GaussianHMM


In [ ]:
model_t1 = hmm.GaussianHMM(n_components= 3, covariance_type="full", n_iter=2000).fit(X)

In [ ]:
print(model_t1)
print(model_t1.n_components)
print(model_t1.transmat_)
print(model_t1.startprob_)
print(model_t1.means_)
print(model_t1.covars_)

print(model_t1.monitor_)
print(model_t1.monitor_.converged)

print(model_t1.n_features)

GaussianHMM Evaluation


In [ ]:
x = X
print(np.exp(model_t1.score(x)))
x = X[:5]
print(x)
print(np.exp(model_t1.score(x)))

In [ ]:
x = [[0.0, 0.0], [5.0, 0.0], [0.0, 5.0], [0.0, 0.0], [5.0, 0.0]]
print(x)
print(np.exp(model_t1.score(x)))

In [ ]:
x = [[0.0, 0.0], [5.0, 0.0], [0.0, 5.0], [0.0, 0.0], [5.0, 0.0]]
print(x)
print(np.exp(model_t1.decode(x)[0]))
print(model_t1.decode(x)[1][:10])

In [ ]:
x = [[0.0, 0.0], [5.0, 0.0], [0.0, 5.0], [0.0, 0.0], [5.0, 0.0]]
print(x)
print(np.exp(model_t1.score_samples(x)[0]))
print(model_t1.score_samples(x)[1])

In [ ]:
x = [[0.0, 0.0], [5.0, 0.0], [0.0, 5.0], [0.0, 0.0], [5.0, 0.0]]
print(x)
print(model_t1.predict(x))

In [ ]:
x = [[0.0, 0.0], [5.0, 0.0], [0.0, 5.0], [0.0, 0.0], [5.0, 0.0]]
print(x)
print(model_t1.predict_proba(x))

In [ ]:
x = [[0.0, 0.0], [5.0, 0.0], [0.0, 5.0], [0.0, 0.0], [5.0, 0.0]]
print(x)
print(np.exp(model_t1.score_samples(x)[0]))
print(model_t1.predict(x))
print(model_t1.score_samples(x)[1])

x = [[0.0, 0.0], [5.0, 0.0], [0.0, 5.0], [0.0, 0.0], [1.0, 0.0]]
print(x)
print(np.exp(model_t1.score_samples(x)[0]))
print(model_t1.predict(x))
print(model_t1.score_samples(x)[1])

x = [[5.0, 0.0], [0.0, 5.0], [0.0, 0.0], [5.0, 0.0], [0.0, 5.0]]
print(x)
print(np.exp(model_t1.score_samples(x)[0]))
print(model_t1.predict(x))
print(model_t1.score_samples(x)[1])

x = [[0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]
print(x)
print(np.exp(model_t1.score_samples(x)[0]))
print(model_t1.predict(x))
print(model_t1.score_samples(x)[1])

GaussianHMM Predict Check


In [ ]:
hidden_states_seq = model_t1.predict(X)
hidden_states_score = model_t1.score_samples(X)
hidden_states_proba = model_t1.predict_proba(X)

In [ ]:
print(hidden_states_score[0])
print(hidden_states_seq)
print(hidden_states_score[1])
print(hidden_states_proba)

In [ ]:
for i in range(0, hidden_states_proba.shape[0]):
    if not np.array_equal(hidden_states_proba[i], hidden_states_score[1][i]):
        print(i)
print('Check Finished!')

In [ ]:
for i, s in enumerate(hidden_states_seq):
    if hidden_states_proba[i,s] < 1:
        print(hidden_states_seq[i])
        print(hidden_states_proba[i])
        print(hidden_states_proba[i,s])

Is the model local optimized?


In [ ]:
iter_num = 10
models = []
scores = []
means = []

for i in range(0, 10):
    model_t = hmm.GaussianHMM(n_components= 3,
                              covariance_type="full",
                              n_iter=2000,
                              random_state=random.randint(0, 100)).fit(X)
    models.append(model_t)
    scores.append(model_t.score(X))
    means.append(model_t.means_)

for i in range(0, 10):
    print(models[i])
    print('Score: {}'.format(scores[i]))
    print('Means: {}'.format(means[i]))

Train GaussianHMM model with fixed parameters


In [ ]:
model_t2 = hmm.GaussianHMM(n_components= 3,
                           covariance_type="full",
                           n_iter=2000,
                           init_params="tmc",
                           params="tmc")
model_t2.startprob_ = np.array([0.4, 0.3, 0.3])
model_t2.fit(X)

In [ ]:
print(model_t2)
print(model_t2.n_components)
print(model_t2.transmat_)
print(model_t2.startprob_)
print(model_t2.means_)
print(model_t2.covars_)

print(model_t2.monitor_)
print(model_t2.monitor_.converged)

print(model_t2.n_features)

In [ ]:
x = X[:5]
print(x)
print(np.exp(model_t1.score(x)))
print(np.exp(model_t2.score(x)))

x = [[0.0, 0.0], [5.0, 0.0], [0.0, 5.0], [0.0, 0.0], [5.0, 0.0]]
print(x)
print(np.exp(model_t1.score(x)))
print(np.exp(model_t2.score(x)))

In [ ]:
x = [[0.0, 0.0], [5.0, 0.0], [0.0, 5.0], [0.0, 0.0], [5.0, 0.0]]
print(x)
print(np.exp(model_t2.score_samples(x)[0]))
print(model_t2.predict(x))
print(model_t2.score_samples(x)[1])

x = [[0.0, 0.0], [5.0, 0.0], [0.0, 5.0], [0.0, 0.0], [1.0, 0.0]]
print(x)
print(np.exp(model_t2.score_samples(x)[0]))
print(model_t2.predict(x))
print(model_t2.score_samples(x)[1])

x = [[5.0, 0.0], [0.0, 5.0], [0.0, 0.0], [5.0, 0.0], [0.0, 5.0]]
print(x)
print(np.exp(model_t2.score_samples(x)[0]))
print(model_t2.predict(x))
print(model_t2.score_samples(x)[1])

x = [[0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]
print(x)
print(np.exp(model_t2.score_samples(x)[0]))
print(model_t2.predict(x))
print(model_t2.score_samples(x)[1])