本文介绍了如何使用hmmlearn在Python中运行隐藏的markov模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用来自GitHub的hmmlearn 来运行二进制隐藏的markov模型.这不起作用:

I tried to use hmmlearn from GitHub to run a binary hidden markov model. This does not work:

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

我收到此错误:

使用此模块的正确方法是什么?请注意,我使用的是与sklearn分开的hmmlearn版本,因为显然sklearn不再维护hmmlearn了.

What is the right way to use this module? Note I am using the version of hmmlearn that was separated from sklearn, because apparently sklearn doesn't maintain hmmlearn anymore.

推荐答案

Fit接受序列列表,而不是单个序列(通常,您可以从不同的运行中观察到多个独立的序列您的实验/观察).因此,只需将您的列表放在另一个列表中

Fit accepts list of sequences and not a single sequence (as in general you can have multiple, independent sequences observed from different runs of your experiments/observations). Thus simply put your list inside another list

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

给予

(-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)

这篇关于如何使用hmmlearn在Python中运行隐藏的markov模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 03:08