0

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
Farzad Vaziri
  • 113
  • 1
  • 7

1 Answers1

0

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

Wei Zhang
  • 334
  • 1
  • 5
  • Thanks for the answer but I think it is zero-based. X[0] = array([31]) – Farzad Vaziri Apr 03 '18 at 21:11
  • Sorry for the confusion. My point is that the value of your `X` should be in [0, 3146] rather than [1, 3147]. Can you verify? – Wei Zhang Apr 03 '18 at 21:19
  • Yes, the values were in the range [1, 3147]. I convert it to the range [0, 3146] and it worked. Thank you soooo much. You saved me ;) actually the trajectories are the sequences of names of places and I just mapped them to integer numbers to make the sequences more readable. Thanks again :) – Farzad Vaziri Apr 03 '18 at 22:13