Python隐形马尔科夫实战_如何使用hmmlearn在Python中运行隐藏的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

I get this error:

ValueError: zero-dimensional arrays cannot be concatenated

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

gives

(-4.125363362578882, array([1, 1, 1, 1, 1]))

MultinomialHMM(algorithm='viterbi',

init_params='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',

n_components=2, n_iter=10,

params='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',

random_state=,

startprob=None, startprob_prior=1.0, thresh=0.01, transmat=None,

transmat_prior=1.0)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值