How to do it...

Let's see how we can build HMMs for sequential data:

  1. Create a new Python file and import the following packages (the full code is given in the hmm.py file that is provided for you):
import numpy as npimport matplotlib.pyplot as plt 
from hmmlearn.hmm import GaussianHMM 
  1. We will use the data from a file named data_hmm.txt that is already provided to you. This file contains comma-separated lines. Each line contains three values: a year, a month, and a piece of floating-point data. Let's load this into a NumPy array:
# Load data from input file 
input_file = 'data_hmm.txt' 
data = np.loadtxt(input_file, delimiter=',') 
  1. Let's stack the data column-wise for analysis. We don't need to technically column-stack this because it's ...

Get Python Machine Learning Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.