scikit-learn GaussianHMM ValueError:输入必须是一个正方形数组 [英] scikit-learn GaussianHMM ValueError: input must be a square array

查看:88
本文介绍了scikit-learn GaussianHMM ValueError:输入必须是一个正方形数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用scikit-learn的GaussianHMM,并在尝试将其适应某些观察时得到以下ValueError.这是演示错误的代码:

I am working with scikit-learn's GaussianHMM and am getting the following ValueError when I try to fit it to some observations. here is code that demonstrates the error:

>>> from sklearn.hmm import GaussianHMM
>>> arr = np.matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> arr
matrix([[1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]])
>>> gmm = GaussianHMM ()
>>> gmm.fit (arr)
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/lib/function_base.py:2005: RuntimeWarning: invalid value encountered in divide
  return (dot(X, X.T.conj()) / fact).squeeze()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Python/2.7/site-packages/sklearn/hmm.py", line 427, in fit
    framelogprob = self._compute_log_likelihood(seq)
  File "/Library/Python/2.7/site-packages/sklearn/hmm.py", line 737, in _compute_log_likelihood
    obs, self._means_, self._covars_, self._covariance_type)
  File "/Library/Python/2.7/site-packages/sklearn/mixture/gmm.py", line 58, in log_multivariate_normal_density
    X, means, covars)
  File "/Library/Python/2.7/site-packages/sklearn/mixture/gmm.py", line 564, in _log_multivariate_normal_density_diag
    + np.dot(X ** 2, (1.0 / covars).T))
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/matrixlib/defmatrix.py", line 343, in __pow__
    return matrix_power(self, other)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/matrixlib/defmatrix.py", line 160, in matrix_power
    raise ValueError("input must be a square array")
ValueError: input must be a square array
>>> 

我该如何补救?看来我正在给它有效的输入.谢谢!

How might I remedy this? It seems that I am giving it valid inputs. Thanks!

推荐答案

根据

According to the docs, gmm.fit(obs) expects obs to be a list of array-like objects:

obs : list
    List of array-like observation sequences (shape (n_i, n_features)).

因此,请尝试:

import numpy as np
from sklearn.hmm import GaussianHMM
arr = np.matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
gmm = GaussianHMM()
print(gmm.fit([arr]))


隐藏的马尔可夫模型(HMM)不再受支持通过sklearn.


Hidden markov models (HMMs) are no longer supported by sklearn.

这篇关于scikit-learn GaussianHMM ValueError:输入必须是一个正方形数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆