[PYTHON] How to infer MAP estimate of HMM with OpenGM

Maximum posteriori estimation of label series with HMM using OpenGM's python interface.

Preparation


import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

import opengm

The data is as follows. Try integerization and smoothing of time series data.

data


d = '17.2 19.7 21.6 21.3 22.1 20.5 16.3 18.4 21.0 16.1 17.5 18.5 18.4 18.3 16.0 21.2 18.8 24.3 23.3 20.5 16.9 22.4 20.1 24.5 24.2 22.7 19.6 23.6 23.3 24.6 25.0 24.3 22.2 22.7 19.5 20.5 17.3 17.2 22.0 20.9 21.5 22.3 24.0 22.4 20.2 15.7 20.4 16.3 17.7 14.3 18.4 16.6 13.9 15.2 14.8 15.0 11.5 13.4 13.5 17.0 15.0 17.5 12.3 11.8 14.5 12.4 12.9 15.8 13.8 11.4 6.5 5.9 7.2 5.6 4.6 7.5 8.9 6.6 3.9 5.7 7.3 6.1 6.8 3.1 2.6 7.9 5.2 2.0 4.0 3.4 5.7 8.1 4.7 5.4 5.9 3.6 2.9 5.7 2.1 1.6 2.3 2.4 1.2 4.2 4.2 2.4 5.6 2.5 3.0 6.1 4.9 7.1 5.0 7.2 5.2 5.1 10.4 8.3 6.9 6.8 7.8 4.2 8.0 3.2 7.9 5.9 9.5 6.4 9.2 11.7 11.6 15.5 16.7'
d = np.array([ float(c) for c in d.split()])

Now, build the HMM model and execute inference.

Run!


nNodes = d.shape[0] #Number of nodes.
nLabels = 20 #Number of discrete classes. 20
variableSpace = np.ones(nNodes)*nLabels #Number of labels for each node. All the same here
gm = opengm.gm(variableSpace)

# unary
for i in range(nNodes):
    u = np.array([ abs(d[i] - j) for j in range(nLabels) ]) #Data term. Absolute value of difference from label
    f = gm.addFunction(u)
    gm.addFactor(f, i)

# pairwise
p = 10 #Cost when the classes of adjacent nodes are different. (0 if same)
pairwise = np.array((np.ma.ones((nLabels,nLabels)) - np.eye(nLabels)) * p) #Pairwise term. 0 for the same label, p for different
f_pw = gm.addFunction(pairwise)
for i in range(nNodes-1):
    gm.addFactor(f_pw, [i, i+1]) #Setting the edge that connects the nodes. Since it is an HMM, it is one-dimensional.

inf = opengm.inference.DynamicProgramming(gm=gm) #Inference algorithm: DP is enough because it is one-dimensional
inf.infer() #Inference execution
res = inf.arg() #Collect results

#plot.
plt.plot(d, label="data")
plt.plot(res, label="result")
plt.legend()

Unknown.png

Recommended Posts

How to infer MAP estimate of HMM with OpenGM
How to infer MAP estimate of HMM with PyStruct
How to implement "named_scope" of RubyOnRails with Django
How to install OpenGM on OSX with macports
How to output CSV of multi-line header with pandas
How to learn structured SVM of ChainCRF with PyStruct
Summary of how to share state with multiple functions
How to estimate kernel density
How to update with SQLAlchemy?
How to cast with Theano
How to Alter with SQLAlchemy?
How to separate strings with','
How to RDP with Fedora31
How to Delete with SQLAlchemy?
How to enable Read / Write of net.Conn with context with golang
How to display a list of installable versions with pyenv
How to cancel RT with tweepy
How to extract features of time series data with PySpark Basics
[Rails] How to display Google Map
[Hugo] Summary of how to add pages to sites built with Learn
Summary of how to use pandas.DataFrame.loc
How to get the ID of Type2Tag NXP NTAG213 with nfcpy
How to use virtualenv with PowerShell
How to install python-pip with ubuntu20.04LTS
How to deal with imbalanced data
Summary of how to use pyenv-virtualenv
Here's a brief summary of how to get started with Django
How to get started with Scrapy
How to get started with Python
How to deal with DistributionNotFound errors
How to monitor the execution status of sqlldr with the pv command
How to Data Augmentation with PyTorch
Explain how to use TensorFlow 2.X with implementation of VGG16 / ResNet50
How to use FTP with Python
Node.js: How to kill offspring of a process started with child_process.fork ()
How to calculate date with python
How to install mysql-connector with pip3
Summary of how to use csvkit
How to INNER JOIN with SQLAlchemy
How to install Anaconda with pyenv
How to authenticate with Django Part 2
How to authenticate with Django Part 3
[EC2] How to take a screen capture of your smartphone with selenium
How to crop the lower right part of the image with Python OpenCV
[Introduction to Python] How to sort the contents of a list efficiently with list sort
[Image recognition] How to read the result of automatic annotation with VoTT
How to deal with SSL error when connecting to S3 with boto of Python
How to deal with garbled characters in json of Django REST Framework
Summary of how to build a LAMP + Wordpress environment with Sakura VPS
How to do arithmetic with Django template
[Blender] How to set shape_key with script
[Python] Summary of how to use pandas
How to title multiple figures with matplotlib
How to speed up instantiation of BeautifulSoup
How to get parent id with sqlalchemy
How to get rid of long comprehensions
How to add a package with PyCharm
How to check the version of Django
How to install CatBoost [as of January 2020]
How to install DLIB with 2020 / CUDA enabled
How to use ManyToManyField with Django's Admin