Closed yarden closed 8 years ago
I think it's just the plotting that fails. The plotting pretty much only works with Gaussian emissions, so that code should really be separated out.
The model instance has a few plotting methods, and while plotting observations won't work with discrete emissions, plotting states should work. Can you try calling model.plot_statseseq(0)
? (If that doesn't work, I can fix up that method to work independently, but you might want to write your own plotting code in the meantime.)
Sorry, I read your question while distracted, so I missed the most important part: you want model.predict
to work!
It looks like you need to pass it a 2D array. Can you try m.predict(np.array([[]]), 5)
?
EDIT: 2D data not required anymore, and that was the wrong shape anyway, see below
Thanks, I tried playing with the array dimension and it didn't seem to work. When I try m.predict(np.array([[]]), 5)
I get:
hmm_states.pyc in aBl(self)
100 aBl = self._aBl = np.empty((data.shape[0],self.num_states))
101 for idx, obs_distn in enumerate(self.obs_distns):
--> 102 aBl[:,idx] = obs_distn.log_likelihood(data).ravel()
103 aBl[np.isnan(aBl).any(1)] = 0.
104
multinomial.pyc in log_likelihood(self, x)
95 def log_likelihood(self,x):
96 err = np.seterr(divide='ignore')
---> 97 out = np.log(self.weights)[x] # log(0) can happen, no warning
98 np.seterr(**err)
99 return out
IndexError: arrays used as indices must be of integer (or boolean) type
Sorry, I guessed the wrong numpy call to make an empty array of the right shape. I think we want np.array([]).reshape(0, 1)
. I'll try to run your example to debug it.
EDIT: 2D data not required anymore
Okay, got your script running. The problem is that Multinomial
doesn't know to return zero potentials when called on np.nan
arguments. The fix is in pybasicbayes (at least, one fix is). One sec...
Can you try with pybasicbayes a2a4499 and pyhsmm 9b8b3f3? I just pushed those. The first should make Multinomial
handle missing observations (nan observations) and the second should make HMM.predict
handle 1D data.
In particular, I tested your script with this call to predict
:
m = models[0]
print m.predict(np.array([]), 100)
that works, thanks! For m.predict(np.array([]), 10)
I get:
(array([ 0., 0., 1., 0., 0., 0., 0., 1., 0., 1.]), array([20, 20, 9, 17, 17, 17, 17, 9, 9, 9], dtype=int32))
(I assume the second array is the index of the hidden states?)
Exactly!
The predict code was a bit clunky because it was hastily written for a particular use case. It can probably be improved further (the seed data should be optional, for one). Let me know if you find other things that need fixing.
I'm trying to run pyhsmm on discrete (categorical) observations. Sampling runs, but using the sampled models gives errors. Code:
Prediction (
m.predict
) fails with the error:and plotting fails with:
I don't care about using plotting; just using it to test if model worked. I mainly want to call
predict
on the sampled models. Any thoughts on how to run pyhsmm with discrete observations? Thanks!