Search code examples
pythonpandasnumpyhmmlearn

Error in model prediction using hmmlearn


Hi I have a dataframe test, I am trying to predict using a Gaussian HMM with hmmlearn.

When I do this:

y = model.predict(test) 
y

I get the hmm working fine producing and array of states

however if i do this:

for i in range(0,len(test)):
    y = model.predict(test[:i])

all I get is y being set to 1.

Can anyone help?

UPDATE

here is the code that does work iterating through

The training set was 0-249:

for i in range(251,len(X)):
    test = X[:i]
    y = model.predict(test)
    print(y[len(y)-1])

Solution

  • HMM models sequences of observations. If you feed a single observation into predict (which does Viterbi decoding by default) you essentially reduce the prediction to the argmax over

    (model.startprob_ * model.predict_proba(test[i:i + 1])).argmax()
    

    which can be dominated by startprob_, e.g. if startprob = [10**-8, 1 - 10**-8]. This could explain the all-ones behaviour you're seeing.