Finding the most likely hidden state sequence with hmmlearn

At this point, we can continue with the previous example, using our model to find the most likely hidden state sequence given a set of possible observations. We can use either the decode() method or the predict() method. The first one returns the log probability of the whole sequence and the sequence itself; however, they all use the Viterbi algorithm as a default decoder:

sequence = np.array([[1], [1], [1], [0], [1], [1], [1], [0], [1], 
[0], [1], [0], [1], [0], [1], [1], [0], [1],
[1], [0], [1], [0], [1], [0], [1], [0], [1],
[1], [1], [0], [0], [1], [1], [0], [1], [1]], dtype=np.int32)

lp, hs = hmm_model.decode(sequence)

print(hs)
[0 1 1 0 1 1 1 0 1 0 1 0 1 0 1 1 0 1 1 0 1 0 1 0 1 0 1 1 1 1 0 1 1 0 1 1]

print(lp)
-30.489992468878615

The sequence is coherent with the transition probability matrix; in fact, it's more likely the persistence of rough weather (1) than the opposite. As a consequence, the transition from 1 to X is less likely than the one from 0 to 1. The choice of state is made by selecting the highest probability; however, in some cases, the differences are minimal (in our example, it can happen to have p = [0.49, 0.51], meaning that there's a high error chance), so it's useful to check the posterior probabilities for all the states in the sequence:

pp = hmm_model.predict_proba(sequence)
print(pp)

[[ 1.00000000e+00 5.05351938e-19] [ 3.76687160e-05 9.99962331e-01] [ 1.31242036e-03 9.98687580e-01] [ 9.60384736e-01 3.96152641e-02] [ 1.27156616e-03 9.98728434e-01] [ 3.21353749e-02 9.67864625e-01] [ 1.23481962e-03 9.98765180e-01]
...

In our case, there are a couple of states that have p ∼ [0.495, 0.505], so even if the output state is 1 (rough weather), it's also useful to consider a moderate probability to observe good weather. In general, if a sequence is coherent with the transition probability previously learned (or manually input), those cases are not very common. I suggest trying different configurations and observations sequences, and to also assess the probabilities for the strangest situations (like a sequence of zero second). At that point, it's possible to retrain the model and recheck the new evidence has been correctly processed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.140.197.10