0

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])
azuric
  • 2,679
  • 7
  • 29
  • 44
  • Are you sure about `y` being all-ones for all `i` in the forloop? This shouldn't be the case at least for the last iteration where `i` is `len(test) - 1`. – Sergei Lebedev Sep 08 '16 at 22:25
  • Hi, the only way I can get it to work is by appending a vector of values incrementally to the training set from the test set and running prediction on the new set. Is that by design or is it possible for the current state/transition mat etc to remain in memory post prediction within the for loop. – azuric Sep 09 '16 at 06:02

1 Answers1

1

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.

Sergei Lebedev
  • 2,659
  • 20
  • 23
  • Ha that makes sense, Does 'map' suffer from the same issue? my hack offers a work around but are there more rigorous methods to solve this? Btw its a great library you have built. – azuric Sep 09 '16 at 07:18
  • Yes, MAP prediction runs forward-backward algorithm, which reduces to the same formula for a single observation. Could you maybe state more precisely what you are after? Is it incremental prediction? – Sergei Lebedev Sep 09 '16 at 07:20
  • Yes it is incremental prediction in real time. I can give you more details by mail if you prefer plus the data etc. – azuric Sep 09 '16 at 07:24
  • This is a [relevant issue](https://github.com/hmmlearn/hmmlearn/issues/144) over at GitHub. I suggest we continue the discussion there. – Sergei Lebedev Sep 09 '16 at 12:48