Search code examples
pythonhidden-markov-modelshmmlearn

Formatting data for hmmlearn


I'm trying to fit a hidden Markov model using hmmlearn in python. I assume that my data is not formatted correctly, however the documentation is light for hmmlearn. Intuitively I would format the data as a 3 dimensional array of n_observations x n_time_points x n_features, but hmmlearn seems to want a 2d array.

import numpy as np
from hmmlearn import hmm
X = np.random.rand(10,5,3)
clf = hmm.GaussianHMM(n_components=3, n_iter=10)
clf.fit(X)

Which gives the following error:

ValueError: Found array with dim 3. Estimator expected <= 2.

Does anyone know how to format data in order to build the HMM I'm after?


Solution

  • Note: All of the following is relevant for the currently unreleased version 0.2.0 of hmmlearn. The version 0.1.0 available on PyPI uses a different API inherited from sklearn.hmm.

    To fit the model to multiple sequences you have to provide two arrays:

    • X --- a concatenation of the data from all sequences,
    • lengths --- an array of sequence lengths.

    I'll try to illustrate these conventions with an example. Consider two 1D sequences

    X1 = [1, 2, 0, 1, 1]
    X2 = [42, 42]
    

    To pass both sequences to the .fit method we need to first concatenate them into a single array and then compute an array of lengths

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