Isa*_*iah 4 python hidden-markov-models hmmlearn
我想使用隐藏马尔可夫模型(解码问题)来预测隐藏状态。数据是分类的。隐藏状态包括饥饿、休息、锻炼和电影。观察范围包括食品、家居、户外和休闲以及艺术和娱乐。我的程序是首先根据观察序列(Baum-Welch 算法)训练 HMM。然后我进行解码(维特比算法)来预测隐藏状态序列。
我的问题是如何将结果(非负整数)映射到相应的类别,例如饥饿或休息。由于训练算法的非确定性,相同数据的每次训练参数都不同。因此,如果我像下面的代码那样进行映射,则每次隐藏状态序列都会不同。
代码如下:
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))
Run Code Online (Sandbox Code Playgroud)
这称为标签切换问题。模型的对数似然对所有状态求和,因此与特定顺序无关。
据我所知,没有通用的方法来处理它。您可以尝试的方法包括:
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2417 次 |
| 最近记录: |