lgd*_*lgd 5 python machine-learning time-series hidden-markov-models hmmlearn
我尝试使用来自 GitHub 的 hmmlearn来运行二进制隐藏马尔可夫模型。这不起作用:
import hmmlearn.hmm as hmm
transmat = np.array([[0.7, 0.3],
[0.3, 0.7]])
emitmat = np.array([[0.9, 0.1],
[0.2, 0.8]])
obs = np.array([0, 0, 1, 0, 0])
startprob = np.array([0.5, 0.5])
h = hmm.MultinomialHMM(n_components=2, startprob=startprob,
transmat=transmat)
h.emissionprob_ = emitmat
# fails
h.fit([0, 0, 1, 0, 0])
# fails
h.decode([0, 0, 1, 0, 0])
print h
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
ValueError:无法连接零维数组
使用此模块的正确方法是什么?注意我使用的是与 sklearn 分离的 hmmlearn 版本,因为显然 sklearn 不再维护 hmmlearn。
Fit 接受序列列表,而不是单个序列(通常,您可以从不同的实验/观察运行中观察到多个独立的序列)。因此,只需将您的列表放入另一个列表中即可
import hmmlearn.hmm as hmm
import numpy as np
transmat = np.array([[0.7, 0.3],
[0.3, 0.7]])
emitmat = np.array([[0.9, 0.1],
[0.2, 0.8]])
startprob = np.array([0.5, 0.5])
h = hmm.MultinomialHMM(n_components=2, startprob=startprob,
transmat=transmat)
h.emissionprob_ = emitmat
# works fine
h.fit([[0, 0, 1, 0, 0]])
# h.fit([[0, 0, 1, 0, 0], [0, 0], [1,1,1]]) # this is the reason for such
# syntax, you can fit to multiple
# sequences
print h.decode([0, 0, 1, 0, 0])
print h
Run Code Online (Sandbox Code Playgroud)
给出
(-4.125363362578882, array([1, 1, 1, 1, 1]))
MultinomialHMM(algorithm='viterbi',
init_params='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
n_components=2, n_iter=10,
params='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
random_state=<mtrand.RandomState object at 0x7fe245ac7510>,
startprob=None, startprob_prior=1.0, thresh=0.01, transmat=None,
transmat_prior=1.0)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
10943 次 |
最近记录: |