隐式马尔可夫算法,简称HMM,马尔可夫认为将来发生的事情与昨天与前一段时间发生的事情有关。它用上一次发生的事情来预测未来。那么这个算法常常用于处理时序数据,比如实时的翻译,语音识别与自然语言处理。
print(__doc__)
import numpy as np
import matplotlib.pyplot as plt
from hmmlearn import hmm
startprob = np.array([0.6,0.3,0.1,0.0])
transmat = 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]])
means = np.array([[0.0, 0.0],
[0.0, 11.0],
[9.0, 10.0],
[11.0, -1.0]])
covars = .5*np.tile(np.identity(2),(4,1,1))
model = hmm.GaussianHMM(n_components=4,covariance_type="full")
model.startprob_ = startprob
model.transmat_ = transmat
model.means_ = means
model.covars_ = covars
x,z=model.sample(500)
plt.plot(x[:,0],x[:,1],".-",label="observations",ms=6,mfc="orange",alpha=0.7)
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')
plt.show()