scikit-learn/examples/plot_hmm_sampling.py

62 lines
2.0 KiB
Python
Raw Normal View History

2012-01-21 14:27:49 +08:00
"""
==================================
Demonstration of sampling from HMM
==================================
2012-03-03 01:07:11 +08:00
This script shows how to sample points from a Hiden Markov Model (HMM):
we use a 4-components with specified mean and covariance.
The plot show the sequence of observations generated with the transitions
between them. We can see that, as specified by our transition matrix,
there are no transition between component 1 and 3.
2012-01-21 14:27:49 +08:00
"""
import numpy as np
import matplotlib.pyplot as plt
2012-03-03 01:07:11 +08:00
from sklearn import hmm
2012-01-21 14:27:49 +08:00
##############################################################
2012-03-03 01:07:11 +08:00
# Prepare parameters for a 3-components HMM
# Initial population probability
start_prob = np.array([0.6, 0.3, 0.1, 0.0])
# The transition matrix, note that there are no transitions possible
# between component 1 and 4
trans_mat = np.array([[0.7, 0.2, 0.0, 0.1],
[0.3, 0.5, 0.2, 0.0],
[0.0, 0.3, 0.5, 0.2],
[0.2, 0.0, 0.2, 0.6]])
# The means of each component
means = np.array([[0.0, 0.0],
[0.0, 11.0],
[9.0, 10.0],
[11.0, -1.0],
])
# The covariance of each component
covars = .5 * np.tile(np.identity(2), (4, 1, 1))
2012-03-03 01:07:11 +08:00
# Build an HMM instance and set parameters
model = hmm.GaussianHMM(4, "full", start_prob, trans_mat,
random_state=42)
# Instead of fitting it from the data, we directly set the estimated
# parameters, the means and covariance of the components
2012-01-21 14:27:49 +08:00
model.means_ = means
model.covars_ = covars
###############################################################
2012-03-03 01:07:11 +08:00
# Generate samples
2012-01-21 14:27:49 +08:00
X, Z = model.sample(500)
2012-03-03 01:07:11 +08:00
# Plot the sampled data
2012-03-03 01:59:50 +08:00
plt.plot(X[:, 0], X[:, 1], "-o", label="observations", ms=6,
2012-03-03 01:07:11 +08:00
mfc="orange", alpha=0.7)
# Indicate the component numbers
for i, m in enumerate(means):
plt.text(m[0], m[1], 'Component %i' % (i + 1),
size=17, horizontalalignment='center',
bbox=dict(alpha=.7, facecolor='w'))
plt.legend(loc='best')
2012-01-21 14:27:49 +08:00
plt.show()