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
The following installation is required.
Python has been confirmed to work with 3 series.
Python >= 2.6
NumPy(tested to work with >= 1.9.3)
SciPy(tested to work with >= 0.16.0)
scikit-learn >= 0.16
hmmlearn can be installed with pip.
$ pip install -U --user hmmlearn
This time we will use Gaussian HMM.
This is a hidden Markov model in which the output of the model is a continuous value.
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
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])
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]])
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
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