2013-12-16 64 views
3

我正在使用scikit-learn的GaussianHMM,當我嘗試將它適用於某些觀察值時,出現以下ValueError錯誤。這裏是顯示錯誤的代碼:scikit-learn GaussianHMM ValueError:輸入必須是方形陣列

>>> 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 
>>> 

我該如何解決這個問題?看來我正在給它有效的輸入。謝謝!

+0

然而,如果我使arr不是方矩陣,即使將它封裝在括號中,我仍然會得到相同的錯誤...即,如果arr是矩陣([[1,2,3], [4,5,6], [7,8,9], [10,11,12]])。有任何想法嗎?謝謝! –

回答

3

你必須適應一個列表,請參閱official examples

>>> gmm.fit([arr]) 
GaussianHMM(algorithm='viterbi', covariance_type='diag', covars_prior=0.01, 
     covars_weight=1, 
     init_params='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 
     means_prior=None, means_weight=0, n_components=1, n_iter=10, 
     params='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 
     random_state=None, startprob=None, startprob_prior=1.0, thresh=0.01, 
     transmat=None, transmat_prior=1.0) 
>>> gmm.n_features 
3 
>>> gmm.n_components 
1 
+0

鏈接已死亡。在這裏看到更新。 http://scikit-learn.org/0.14/auto_examples/applications/plot_hmm_stock_analysis.html – lifelogger

3

the docsgmm.fit(obs)預計obs是一個列表陣列狀物體的

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])) 

隱馬爾可夫模型(隱馬爾可夫模型)由sklearn提供no longer supported

+0

所以我不清楚的是如果我們必須擁有儘可能多的列作爲排放可能性,那麼在每一列中代表什麼?爲什麼我們不能通過一維排放序列? – Brooks

相關問題