0

I'm trying to use hmmlearn to get the most likely hidden state sequence from a Hidden Markov Model, given start probabilities, transition probabilities, and emission probabilities.

I have two hidden states and four possible emission values, so I'm doing this:

num_states = 2
num_observations = 4
start_probs = np.array([0.2, 0.8])
trans_probs = np.array([[0.75, 0.25], [0.1, 0.9]])
emission_probs = np.array([[0.3, 0.2, 0.2, 0.3], [0.3, 0.3, 0.3, 0.1]])

model = hmm.MultinomialHMM(n_components=num_states)
model.startprob_ = start_probs
model.transmat_ = trans_probs
model.emissionprob_ = emission_probs

seq = np.array([[3, 3, 2, 2]]).T

model.fit(seq)
log_prob, state_seq = model.decode(seq)

My stack trace points to the decode call and throws this error:

ValueError: too many values to unpack (expected 2)

I thought decode (looking at the docs) returns a log probability and the state sequence, so I'm confused.

Any idea?

Thanks!

anon_swe
  • 8,791
  • 24
  • 85
  • 145

1 Answers1

1

The call model.fit(seq) requires seq to be a list of lists, as you correctly set it up like this. However, model.decode(seq) requires seq to only be a list, not a list of lists. Thus,

model.fit([[3, 3, 2, 2]])
log_prob, state_seq = model.decode([3, 3, 2, 2])

should work without throwing an error. See also here.

The error ValueError: too many values to unpack (expected 2) is thrown from a function called by a function called by a function... inside decode. So, the error does not mean that the number of returned objects of decode was wrong, but from framelogprob.shape somewhere inside the base.py. A more meaningful error message would make life easier here. I had the same issue and it drove me crazy. Hope my post helps somebody.

Nadine
  • 11
  • 1