machine-learningsequenceshidden-markov-modelsmultinomialhmmlearn

error while fitting a hmm.MultinomialHMM with sequences of observed data


I'm trying to fit a multinomial model with my observed data. I have a dataset containing trajectories with different lengths. Since my observations are discrete I try to fit with a multinomial model. The number of observation symbols is 3147 and the number of trajectories(sequences) is 4760. Whilst I give the observation sequences (X with an array shape defined in hmmlearn class) and the length of observation with () to fit method with the this code:

X
array([[31],
       [ 1],
       [17],
       ..., 
       [ 4],
       [ 1],
       [16]])

lengths
[28,
 6,
 11,
 7,
 2,
 2,
 ...]

model = hmm.MultinomialHMM(n_components=10).fit(X, lengths)

I got an error. Can someone help me and explain what I am doing wrong. Thanks.

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-72-565aadfd0ae4> in <module>()
----> 1 model = hmm.MultinomialHMM(n_components=10).fit(X, lengths)

C:\Python\Anaconda2\lib\site-packages\hmmlearn\base.pyc in fit(self, X, lengths)
    427             curr_logprob = 0
    428             for i, j in iter_from_X_lengths(X, lengths):
--> 429                 framelogprob = self._compute_log_likelihood(X[i:j])
    430                 logprob, fwdlattice = self._do_forward_pass(framelogprob)
    431                 curr_logprob += logprob

C:\Python\Anaconda2\lib\site-packages\hmmlearn\hmm.pyc in _compute_log_likelihood(self, X)
    403 
    404     def _compute_log_likelihood(self, X):
--> 405         return np.log(self.emissionprob_)[:, np.concatenate(X)].T
    406 
    407     def _generate_sample_from_state(self, state, random_state=None):

IndexError: index 3147 is out of bounds for axis 1 with size 3147


Solution

  • The value of your X should start from zero rather than from one.