-2

I am trying to run a hidden markov model, however the fit function doesn't work properly.

Code:

import numpy as np
from hmmlearn import hmm

X1 = [[0.5], [1.0], [-1.0], [0.42], [0.24]]
X2 = [[2.4], [4.2], [0.5], [-0.24]]

X = np.concatenate([X1, X2])
lengths = [len(X1), len(X2)]

hmm.GaussianHMM(n_components=3).fit(X, lengths) 

I get this error message:

TypeError                                 Traceback (most recent call last)
<ipython-input-16-cdfada1be202> in <module>()
      8 lengths = [len(X1), len(X2)]
      9 
---> 10 hmm.GaussianHMM(n_components=3).fit(X, lengths)

TypeError: fit() takes 2 positional arguments but 3 were given
Vivek Kumar
  • 35,217
  • 8
  • 109
  • 132

2 Answers2

0

Please check the version of hmmlearn you have and update that. The lengths param is available in newer versions as seen here

http://hmmlearn.readthedocs.io/en/latest/api.html#hmmlearn.hmm.GaussianHMM.fit

Then try doing (as @Harpal suggested):

hmm.GaussianHMM(n_components=3).fit(X, lengths=lengths)
Vivek Kumar
  • 35,217
  • 8
  • 109
  • 132
  • I checked it and have the newest version. However, when clicking on the source/code, it can be seen that the function expect his: def fit(self, X, lengths=None) – Joep van der Plas Mar 29 '18 at 14:19
  • @JoepvanderPlas I am on version 0.2.0 and not getting any error on your code. Can you tell your version? – Vivek Kumar Mar 29 '18 at 14:22
  • @JoepvanderPlas I am not able to duplicate this. I have hmmlearn 0.2.0 and scikit-learn 0.19.1. Checked both on python2 and python3. Are you sure you are using the same versino which you are telling here. Maybe you installed it on other location. Try `import hmmlearn` and `print(hmmlearn.__version__)`. from the current script. – Vivek Kumar Mar 29 '18 at 14:42
0

This error can be reproduced for hmmlearn 0.1.1,

however if you do a pip install hmmlearn==0.2.0 in your virtual env and follow up with hmm.GaussianHMM(n_components=3).fit(X, lengths=lengths).

Things should work out just fine!

Noslav
  • 1
  • 1