python - hmmlearn(隐马尔可夫模型)解码后如何将隐藏状态映射到相应的类别?

标签 python hidden-markov-models hmmlearn

我想使用隐马尔可夫模型预测隐藏状态(解码问题)。数据是分类的。隐藏状态包括饥饿、休息、锻炼和电影。观察集包括食物、家庭、户外和娱乐以及艺术和娱乐。我的程序是先根据观察序列(Baum-Welch算法)训练HMM。然后我进行解码(Viterbi 算法)以预测隐藏状态序列。

我的问题是如何将结果(非负整数)映射到它们相应的类别,如饥饿或休息。由于训练算法的不确定性,相同数据的每次训练参数都不同。因此,如果我像下面的代码那样做 map ,隐藏状态序列每次都是不同的。

代码如下:

from __future__ import division
import numpy as np
from hmmlearn import hmm

states = ["Hungry", "Rest", "Exercise", "Movie"]
n_states = len(states)

observations = ["Food", "Home", "Outdoor & Recreation", "Arts & Entertainment"]

# The number in this sequence is the index of observation
category_sequence = [1, 0, 1, 2, 1, 3, 1]
Location = np.array([category_sequence]).T
model = hmm.MultinomialHMM(n_components=n_states).fit(Location)

logprob, result = model.decode(Location)
print "Category:", ", ".join(map(lambda x: observations[x], Location.T[0]))
print "Intent:", ", ".join(map(lambda x: states[x], result))

最佳答案

这被称为标签转换问题。模型的对数似然对所有状态求和,因此与特定顺序无关。

据我所知,没有处理它的通用方法。您可能会尝试的事情包括:

  • 找到部分标记的数据集,对其运行 predict 并使用预测将状态索引映射到相应的标签。
  • 针对每个状态下可能的参数值提出启发式方法。对于多项式,这可能会很棘手,但如果您建模,例如加速度计数据。

更新:从标记数据猜测状态到标签映射的临时版本。

def guess_labels(hmm, X, labels):
    result = [None] * hmm.n_components
    for label, y_t in zip(labels, hmm.predict(X)):
        assigned = result[y_t]
        if assigned is not None:
            # XXX clearly for any real data there might be
            #     (and there will be) conflicts. Here we just blindly
            #     hope the ``assert`` never fires.
            assert assigned == label
        else:
            result[y_t] = label
    return result

关于python - hmmlearn(隐马尔可夫模型)解码后如何将隐藏状态映射到相应的类别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39756006/

相关文章:

python - 我可以向量化这个Python代码吗?

machine-learning - 如何使用 hmmlearn 解决基本的 HMM 问题

r - R中的隐马尔可夫模型包

java - 分层HashMap N阶HMM实现

python - 在 Python 中使用 hmmlearn 学习字符序列

python - 一次沿轴应用函数 n 项

javascript - 多阶段范围验证

python - 作为Python中的应用程序页面发布到Facebook应用程序页面墙

python - 为 python hmmlearn 包编译 C 代码时出错