hmmlearn

0.3.3 · active · verified Mon Apr 13

hmmlearn is a Python library for unsupervised learning of Hidden Markov Models (HMMs) with an API designed to be compatible with scikit-learn. It includes implementations for Gaussian HMMs, Multinomial HMMs, and GMM-HMMs. The current version is 0.3.3, and it receives updates for bug fixes and compatibility, though major feature releases are infrequent.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create, train, and use a Gaussian Hidden Markov Model (HMM). It covers generating sample data, fitting the HMM, predicting hidden states, scoring the model, and sampling new sequences from the learned model.

import numpy as np
from hmmlearn import hmm

# 1. Generate some sample data
# Let's create data that has two underlying 'states' with different means
# The HMM will try to discover these.
np.random.seed(42)
X = np.concatenate([np.random.randn(100, 1) + 0,
                    np.random.randn(100, 1) + 5])
# Mix the data to simulate a sequence, important for HMMs
np.random.shuffle(X)

# 2. Create and train a Gaussian HMM model
# n_components: number of hidden states we want to find
# covariance_type: "full", "tied", "diag", "spherical"
# n_iter: number of EM iterations to perform
model = hmm.GaussianHMM(n_components=2, covariance_type="full", n_iter=100, random_state=42)

# The 'fit' method estimates parameters from data using the EM algorithm.
# If initial parameters are not set, 'hmmlearn' will use k-means to initialize.
model.fit(X)

# 3. Predict the hidden states for the observed data
hidden_states = model.predict(X)

print("Learned Means:\n", model.means_)
print("Learned Transition Matrix:\n", model.transmat_)
print("First 10 hidden states:\n", hidden_states[:10])

# 4. Score the model (log-likelihood of the data given the model)
score = model.score(X)
print("\nModel score (log-likelihood):", score)

# 5. Generate new samples from the learned model
X_new, Z_new = model.sample(n_samples=50)
print("\nShape of new samples:", X_new.shape)
print("First 5 new samples:\n", X_new[:5].T)
print("First 5 new hidden states:\n", Z_new[:5])

view raw JSON →