gpt4 book ai didi

python - 将数据拟合到 hmm.MultinomialHMM

转载 作者:太空宇宙 更新时间:2023-11-04 10:15:10 42 4
gpt4 key购买 nike

我正在尝试使用 hmmlearn 库预测给定一些数据的最佳序列,但出现错误。我的代码是:

from hmmlearn import hmm
trans_mat = np.array([[0.2,0.6,0.2],[0.4,0.0,0.6],[0.1,0.2,0.7]])
emm_mat = np.array([[0.2,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1],[0.1,0.1,0.1,0.1,0.2,0.1,0.1,0.1,0.1],[0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.2]])
start_prob = np.array([0.3,0.4,0.3])
X = [3,4,5,6,7]
model = GaussianHMM(n_components = 3, n_iter = 1000)
X = np.array(X)
model.startprob_ = start_prob
model.transmat_ = trans_mat
model.emissionprob_ = emm_mat

# Predict the optimal sequence of internal hidden state
x = model.fit([X])

print(model.decode([X]))

但我收到一条错误消息:

Traceback (most recent call last):
File "hmm_loyalty.py", line 55, in <module>
x = model.fit([X])
File "build/bdist.macosx-10.6-x86_64/egg/hmmlearn/base.py", line 421, in fit
File "build/bdist.macosx-10.6-x86_64/egg/hmmlearn/hmm.py", line 183, in _init
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sklearn/cluster/k_means_.py", line 785, in fit
X = self._check_fit_data(X)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sklearn/cluster/k_means_.py", line 758, in _check_fit_data
X.shape[0], self.n_clusters))
ValueError: n_samples=1 should be >= n_clusters=3

任何人都知道这意味着什么以及我可以做些什么来解决它?

最佳答案

您的代码存在许多问题:

  1. 模型 是一个GaussianHMM。您可能需要 MultinomialHMM
  2. 输入的 X 形状错误。对于 MultinomialHMM,X 必须具有形状 (n_samples, 1),因为观察是一维的。
  3. 除非需要估计某些模型参数,否则您不需要拟合,而这里不是这种情况。

这是一个工作版本

import numpy as np
from hmmlearn import hmm

model = hmm.MultinomialHMM(n_components=3)
model.startprob_ = np.array([0.3, 0.4, 0.3])
model.transmat_ = np.array([[0.2, 0.6, 0.2],
[0.4, 0.0, 0.6],
[0.1, 0.2, 0.7]])
model.emissionprob_ = np.array([[0.2, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
[0.1, 0.1, 0.1, 0.1, 0.2, 0.1, 0.1, 0.1, 0.1],
[0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.2]])

# Predict the optimal sequence of internal hidden state
X = np.atleast_2d([3, 4, 5, 6, 7]).T
print(model.decode(X))

关于python - 将数据拟合到 hmm.MultinomialHMM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35806469/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com