【问题标题】:Formatting data for hmmlearn为 hmmlearn 格式化数据
【发布时间】:2016-01-15 17:23:58
【问题描述】:

我正在尝试在 python 中使用 hmmlearn 来拟合隐藏的马尔可夫模型。我假设我的数据格式不正确,但是文档对于 hmmlearn 来说很简单。直观地说,我会将数据格式化为 n_observations x n_time_points x n_features 的 3 维数组,但 hmmlearn 似乎想要一个 2d 数组。

import numpy as np
from hmmlearn import hmm
X = np.random.rand(10,5,3)
clf = hmm.GaussianHMM(n_components=3, n_iter=10)
clf.fit(X)

这给出了以下错误:

ValueError: Found array with dim 3. Estimator expected <= 2.

有谁知道如何格式化数据以构建我所追求的 HMM?

【问题讨论】:

    标签: python hidden-markov-models hmmlearn


    【解决方案1】:

    注意:以下所有内容均与 hmmlearn 的当前未发布的 0.2.0 版本相关。 PyPI 上可用的 0.1.0 版本使用从 sklearn.hmm 继承的不同 API。

    fit模型到多个序列你必须提供两个数组:

    • X --- 所有序列的数据串联,
    • lengths --- 序列长度数组。

    我将尝试通过一个示例来说明这些约定。考虑两个一维序列

    X1 = [1, 2, 0, 1, 1]
    X2 = [42, 42]
    

    要将这两个序列传递给.fit 方法,我们需要首先将它们连接成一个数组,然后计算一个长度数组

    X = np.append(X1, X2)
    lengths = [len(X1), len(X2)]
    

    【讨论】:

      【解决方案2】:

      在单个时间序列观察的情况下,hmmlearn fit 方法期望数据位于可以使用 reshape(-1,1) 获得的二维列向量中:

      X = np.array([1, 1, 0, -1, -1])
      model = hmm.GaussianHMM(n_components=2, n_iter=100)
      model.fit(X.reshape(-1,1))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-04-26
        • 1970-01-01
        • 1970-01-01
        • 2020-07-07
        • 1970-01-01
        • 2016-07-12
        • 2019-12-05
        • 1970-01-01
        相关资源
        最近更新 更多