How to use hmmlearn, a Python library that realizes hidden Markov models

I couldn't find the Japanese information of hmmlearn, so I summarized the minimum usage.

hmmlearn is a scikit-learn-like Python library that realizes HMM (Hidden Markov Model). http://hmmlearn.readthedocs.org

setup

The following installation is required.

Installation

hmmlearn can be installed with pip.

$ pip install -U --user hmmlearn

How to use

This time we will use Gaussian HMM.

What is Gaussian HMM?

This is a hidden Markov model in which the output of the model is a continuous value.

Required parameters

Create an instance of model by the following method.

from hmmlearn import hmm

model = hmm.GaussianHMM(n_components=3, covariance_type="full")

The argument n_componets specifies the number of states, and covariance_type specifies the type of covariance.

The attributes that must be specified are as follows.

This time, in the example, the number of states is set to 5 and the parameters are entered.

import numpy as np
from hmmlearn import hmm


startprob = np.array([0.0, 0.1, 0.8, 0.1, 0.0])
transmat = np.array([[0.9, 0.1, 0.0, 0.0, 0.0],
                     [0.4, 0.3, 0.3, 0.0, 0.0],
                     [0.1, 0.2, 0.4, 0.3, 0.0],
                     [0.0, 0.1, 0.3, 0.3, 0.3],
                     [0.0, 0.0, 0.3, 0.1, 0.6]])

means = np.array([[1.0, 1.0],
                  [2.0, 2.0],
                  [3.0, 3.0],
                  [4.0, 4.0],
                  [5.0, 5.0]])

covars = 0.1 * np.tile(np.identity(2), (5, 1, 1))

model = hmm.GaussianHMM(n_components=5, covariance_type="full")

model.startprob_ = startprob
model.transmat_ = transmat
model.means_ = means
model.covars_ = covars

Forecast

Let's predict the state series with sample data.

>>> X, Z = model.sample(10)

>>> print(X)
[[ 3.42184743  2.84259768]
 [ 1.83591345  2.60456015]
 [ 3.0610817   3.67242462]
 [ 3.88970955  3.94599461]
 [ 4.99679025  5.21663028]
 [ 5.05294062  4.80691392]
 [ 5.25836686  5.08960965]
 [ 5.49849546  5.00469741]
 [ 5.31740667  4.77140169]
 [ 4.1283215   5.06959465]]

>>> print(Z)
[2 1 2 3 4 4 4 4 4 4]

This is the result that the state series is predicted to be Z when the sample data is X.

Let's predict the state series from non-sample data. Use the predict method to predict a state series based on an output series.

>>> model.predict([[1.3], [2.0], [4.3], [4.2]])
array([1, 2, 3, 3])

Learning

I will show you how to estimate the parameters by learning.

Training is performed by the fit method and parameter estimation is performed. This time, X is used as training data.

import numpy as np
from hmmlearn import hmm

model = hmm.GaussianHMM(n_components=5, covariance_type="full")
X = np.array([[1.2], [2.3], [1.1], [4.2], [3.3]])
model.fit(X)

You can confirm that the parameters have been entered by learning.

>>> model.startprob_
array([  2.06946712e-292,   0.00000000e+000,   3.13184994e-003,
         1.27702612e-300,   9.96868150e-001])
        
>>> model.means_
array([[ 2.3       ],
       [ 4.2       ],
       [ 1.10171369],
       [ 3.3       ],
       [ 1.15485602]])
       
>>> model.covars_
array([[[ 0.01      ]],
       [[ 0.01      ]],
       [[ 0.05488646]],
       [[ 0.01      ]],
       [[ 0.00797925]]])
       
>>> model.transmat_
array([[  1.27206138e-223,   1.57268167e-192,   1.79623272e-001,
          9.10036066e-211,   8.20376728e-001],
       [  5.88225536e-075,   2.07494122e-025,   1.88590294e-063,
          1.00000000e+000,   5.18696363e-119],
       [  1.71368655e-002,   9.82863134e-001,   6.65061859e-190,
          9.25847733e-045,   3.48719264e-293],
       [  1.79414943e-074,   2.06495338e-025,   3.28132831e-011,
          1.00000000e+000,   1.50081312e-010],
       [  5.48560165e-001,   4.51439835e-001,   1.32144625e-190,
          6.49028218e-045,   0.00000000e+000]])

Evaluation

Find the log-likelihood to evaluate the model.

The score method evaluates the model. Specify the output series for evaluation as X.

>>> X = np.array([[3.2], [2.4], [3.1], [3.2], [3.0], [3.9], [4.0]])
>>> model.score(X)
0.11878179338435844

Export and import

Exports and imports the model.

You can export the model by using sklearn's joblib.

>>> from sklearn.externals import joblib
>>> joblib.dump(remodel, "filename.pkl")
["filename.pkl"]

Similarly, it can be read by the load method using joblib.

>>> from sklearn.externals import joblib
>>> joblib.load('filename.pkl')
GaussianHMM(algorithm='viterbi', covariance_type='full', covars_prior=0.01,
      covars_weight=1, init_params='stmc', means_prior=0, means_weight=0,
      min_covar=0.001, n_components=5, n_iter=10, params='stmc',
      random_state=None, startprob_prior=1.0, tol=0.01, transmat_prior=1.0,
      verbose=False)

Recommended Posts

How to use hmmlearn, a Python library that realizes hidden Markov models
How to use Requests (Python Library)
How to use the C library in Python
How to use Python Image Library in python3 series
How to install a Python library that can be used by pharmaceutical companies
[Python] How to use the graph creation library Altair
[Python] How to write a docstring that conforms to PEP8
[Python] A convenient library that converts kanji to hiragana
python3: How to use bottle (2)
[Python] How to use list 1
How to use Python argparse
Python: How to use pydub
[Python] How to use input ()
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Python bytes
How to use pip, a package management system that is indispensable for using Python
How to use a library that is not originally included in Google App Engine
[python] How to use the library Matplotlib for drawing graphs
How to use the __call__ method in a Python class
How to write a metaclass that supports both python2 and python3
Use pymol as a python library
How to write a Python class
Python: How to use async with
[Python] How to use Pandas Series
How to use SQLite in Python
[Python] How to import the library
[Python] How to use list 3 Added
How to use Mysql in python
How to use OpenPose's Python API
How to use ChemSpider in Python
How to use FTP with Python
Python: How to use pydub (playback)
How to use PubChem in Python
How to use python zip function
[Python] How to use Typetalk API
I want to use a wildcard that I want to shell with Python remove
Use networkx, a library that handles graphs in python (Part 2: Tutorial)
[Introduction to Python] How to use the in operator in a for statement?
[For beginners] How to register a library created in Python in PyPI
I made a library that adds docstring to a Python stub file.
How to trick and use a terrible library that is supposed to be kept globally in flask
[Introduction to Python] How to use class in Python?
[Python] Use pandas to extract △△ that maximizes ○○
How to install and use pandas_datareader [Python]
[Python] How to make a class iterable
[Python] How to convert a 2D list to a 1D list
[python] How to use __command__, function explanation
[Python] How to invert a character string
How to get a stacktrace in python
[Python] Organizing how to use for statements
Memorandum on how to use gremlin python
[Python2.7] Summary of how to use unittest
python: How to use locals () and globals ()
How to use __slots__ in Python class
How to use "deque" for Python data
How to use Python zip and enumerate
[Python] Understand how to use recursive functions
Summary of how to use Python list