Search code examples
pythonhmmlearn

HMMlearn Gaussian Mixture: set mean, weight and variance of each mixture component


I am using the HMMlearn module to generate a HMM with a Gaussian Mixture Model.

The problem is I want to initialize the mean, variance and weight of each mixture component before I fit the model to any data.

How would I go about doing this?


Solution

  • From the HHMlean documentation

    Each HMM parameter has a character code which can be used to customize its initialization and estimation. EM algorithm needs a starting point to proceed, thus prior to training each parameter is assigned a value either random or computed from the data. It is possible to hook into this process and provide a starting point explicitly. To do so

    1. ensure that the character code for the parameter is missing from init_params and then
    2. set the parameter to the desired value.

    Here is an example:

    model = hmm.GaussianHMM(n_components=3, n_iter=100, init_params="t")
    model.startprob_ = np.array([0.6, 0.3, 0.1])
    model.means_ = np.array([[0.0, 0.0], [3.0, -3.0], [5.0, 10.0]])
    model.covars_ = np.tile(np.identity(2), (3, 1, 1))
    

    Another example for initializing a GMMHMM

    model = hmm.GMMHMM(n_components=3, n_iter=100, init_params="smt")
    model.gmms_ = [sklearn.mixture.GMM(),sklearn.mixture.GMM(),sklearn.mixture.GMM()]
    

    The GMMs themselves can be initialised in a very similar way using its attributes and by providing in the init_params string, which attributes should be initialize by the constructor.