Publish AI, ML & data-science insights to a global community of data professionals.

Auto-HMM in Python

Automatic Model selection, training, and testing for the hidden Markov Model

Photo by Martin Sanchez on Unsplash
Photo by Martin Sanchez on Unsplash

During my Ph.D. at UCLA, I developed various models for sequential and time-series data. The hidden Markov model (HMM) was one of the earliest models I used, which worked quite well. I could not find any tutorial or any working codes on the HMM in Python/MATLAB/R. I am releasing the Auto-HMM, which is a python package to perform automatic model selection using AIC/BIC for supervised and unsupervised HMM.

This package uses hmmlearn for hidden Markov model training and decoding and it includes a model selection for the optimal number of parameters (number of mixture components, number of hidden states, etc). I implemented a similar package in MATLAB and R.

Please comment under this post if you would like to see the R and MATLAB implementation of the same model.

The training in HMM is done through the Baum-Welch, which is the special case of the EM algorithm. The decoding is done through the Viterbi algorithm. I guess the hmmlearn package supports the MAP decoder besides the ML (Viterbi) decoder.

The model selection is done through AIC and BIC, which operate by penalizing the likelihood functions. This is done automatically here by specifying the maximum number of hidden states you like and the algorithm finds the optimal number of hidden states for the discrete HMM, and optimal number mixture component, and an optimal number of hidden states for the continuous HMM.

To access the code please visit my GitHub at

GitHub – manitadayon/Auto_HMM: Hidden Markov Model

Please go over the examples in DHMM_Testing and HMM_Testing Python files. They require you to have provided your CSV file address, number of iteration for HMM model, training size, number of features (dimension of time series).

As an example, the following code performs the discrete HMM for 2000 observations with 50 points in time for a maximum of 3 hidden states. The training size is set to 0.8 (0.8 * 2000 = 1600). The Feat determines the dimensionality of the time series (1 means a univariate time series). The flag determines if you would like to sort your hidden states in descending or ascending order.

from Hidden_Markov_Model import *
from Hidden_Markov_Model.DHMM import *
Train_ratio=0.8
Max_state=3
Iter=1000
Feat=1
N=2000
T=50
flag=0
N_symb=3
Path= 'Path to CSV file'
Data=pd.read_csv(Path)
Data=Data.astype(int)
First_DHMM=Supervised_DHMM(Train_ratio,Max_state,Iter,Feat,N,T,Data,N_symb)
First_DHMM.Best_States()

For more information about my recent activity and any questions, please visit my YouTube page. I have also written tsBNgen, the Python package for generating synthetic time-series data, which can be used as the input to the Auto-HMM model. To learn more about tsBNgen, please visit my YouTube page below.

AI and ML Fundamentals

If you have any questions or concerns you can visit my personal page at

Home – Manie Tadayon

To learn more about the HMM modeling and implementation please refer to the following video:


Towards Data Science is a community publication. Submit your insights to reach our global audience and earn through the TDS Author Payment Program.

Write for TDS

Related Articles