# How do I train HMM's for classification?

So I understand that when you train HMM's for classification the standard approach is:

1. Separate your data sets into the data sets for each class
2. Train one HMM per class
3. On the test set compare the likelihood of each model to classify each window

But how do I train the HMM on each class? Do I just concatenate the data pertaining to one class together? But isn't the time series data meant to be sequential - and if I do that then I am saying that some data points are consecutive when they are not?

To be more concrete I have some EEG data which is a 96xT matrix where I have 96 feature vectors which are the power spectral densities of different frequencies from different channels and T is the length of time of the signal (at some sampling rate)

This can be divided into windows which I know from the experimental protocol (the data is labelled) and so I can gather together sets of 96*t matrices for each class. Where t is less than T and denotes the size of each window.

How do I then train the HMM on this data? If it helps I am trying to use the pmtk3 toolkit, but I open to using anything really - it just has to be able to deal with real-valued observations as the power spectral densities are continuous not discrete (the default MATLAB toolbox can only deal with discrete observations).

The aim is to be able to classify EEG data windows to a given mental state having trained upon the labelled data. It is a brain-computer interface problem using the Berlin BCI Competition data.

• Does this page suit what you need ? Mar 25, 2014 at 15:22
• It seems to - I'll try it out to make sure. It definitely allows for Gaussian and Mixture of Gaussian models rather than just multinomials. And it appears you can globally train a model on multiple observations So it looks good. I've been meaning to learn Python properly anyway as it has a much broader use. Thanks :) Mar 25, 2014 at 17:39

The approach you describe for using HMMs for classification is really only applicable to settings where you have independent sequences you want to classify. For example, if I was classifying the sentiment of sentences as positive or negative, I could build an HMM for each as you've described. See the related answer I gave here. Notice how this rests on the assumption I can break sequences up into the meaningful chunks to be classified before I compare posteriors. This doesn't seem to be the case for you problem since you effectively have one large length $T$ time series. Here's what I would try.

You mentioned over at reddit that you were hesitant to assign a single state for each class. Have you tried this? It may not work as poorly as you think. The estimation problem is also significantly easier in this case. Estimating the transition probabilities is easy, you just count essentially. Furthermore you can just fit the emission probabilities for each state based on you're observed data and corresponding class, ignoring the temporal aspects.

If you are convinced this is a bad idea, or find it performs poorly but still want to stick with generative models, you could use something like a Hierarchical HMM. For example, you could let the states in the top-level represent the classes and then allow the lower level HMMs to model the temporal variation within classes. You could also use one big HMM to achieve something similar. If you have $K$ classes, allocate $N$ states for each class (so $N \times K$ states altogether) of the form $s_{ki}$, $k = 1, \ldots, K$, $i=1,\ldots N$. During training you would need to force the HMM to only assign positive probability to transitioning to a state at time $t$ where $k$ matches the label at time $t$. I might have worded this a little awkwardly so I hope it is clear what I mean. Obviously you can generalize this to having different numbers of states per class. There are probably other types of Dynamic Bayesian Networks you could use as well. Kevin Murphy's thesis is an excellent reference. He also discusses converting HHMMs to HMMs.

Lastly, you could switch to a discriminative model like a Conditional Random Field. A discriminative model will allow you to easily incorporate more complex features and more directly addresses the problem at hand (estimating conditional densities). This is probably what I'd try first.

• The observed values are continuous so I would fit a gaussian or GMM. As discussed at reddit this limits the ability of the model. I am using this model to see how the temporal aspect effects the classification accuracy compared to static models. I am aware that discriminative models might be better for classification and have read bits of Murphy's thesis. This seems interesting but the HMM was recommended to me by my professor (it's an open-ended project) so I will try that. I want to work more with EEG/MEG signals in the future though so I will try the other methods at some point. :) Mar 25, 2014 at 23:52
• In the end I used sklearn and its support for multiple observations. It seemed to work correctly. I should dig into the code and see how it actually deals with it. Jul 14, 2014 at 12:58
• Alex, could you provide a link or example to what you refer as "sklearn and its support for multiple observations"? I've been dealing with this problem (and the related problem of how to "tell" an HMM that multiple observations belong to a single class label) for many years now and would love to know how you addressed them. Oct 27, 2015 at 0:38