data
是數據的一維數組。將高斯混合模型擬合到單一特徵數據的正確方法是什麼?
data = [0.0, 7000.0, 0.0, 7000.0, -400.0, 0.0, 7000.0, -400.0, -7400.0, 7000.0, -400.0, -7000.0, -7000.0, 0.0, 0.0, 0.0, -7000.0, 7000.0, 7000.0, 7000.0, 0.0, -7000.0, 6600.0, -7400.0, -400.0, 6600.0, -400.0, -400.0, 6600.0, 6600.0, 6600.0, 7000.0, 6600.0, -7000.0, 0.0, 0.0, -7000.0, -7400.0, 6600.0, -400.0, 7000.0, -7000.0, -7000.0, 0.0, 0.0, -400.0, -7000.0, -7000.0, 7000.0, 7000.0, 0.0, -7000.0, 0.0, 0.0, 6600.0, 6600.0, 6600.0, -7400.0, -400.0, -2000.0, -7000.0, -400.0, -7400.0, 7000.0, 0.0, -7000.0, -7000.0, 0.0, -400.0, -7400.0, -7400.0, 0.0, 0.0, 0.0, -400.0, -400.0, -400.0, -400.0, 6600.0, 0.0, -400.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -400.0, -400.0, 0.0, 0.0, -400.0, -400.0, 0.0, -400.0, 0.0, -400.0]
我想適合一些gaussians這個數據並繪製它們。
如果我運行
import numpy as np
from sklearn import mixture
x = np.array(data)
clf = mixture.GaussianMixture(n_components=2, covariance_type='full')
clf.fit(x)
我得到的錯誤
ValueError: Expected n_samples >= n_components but got n_components = 2, n_samples = 1
和
DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and will raise ValueError in 0.19. Reshape your data either using X.reshape(-1, 1) if your data has a single feature or X.reshape(1, -1) if it contains a single sample.
好吧......我可以這樣生活。警告告訴我該怎麼做。但是,如果我跑
x = np.array(data).reshape(-1,1)
clf = mixture.GaussianMixture(n_components=2, covariance_type='full')
clf.fit(x)
我得到的錯誤
ValueError: Expected the input data X have 1 features, but got 32000 features
我在做什麼錯?什麼是正確的方式?
編輯:
我才意識到,我誤讀了錯誤信息。不是fit()
正在降雨的錯誤,但score_samples()
。
我試圖在事後繪製高斯。
x = np.linspace(-8000,8000,32000)
y = clf.score_samples(x)
plt.plot(x, y)
plt.show()
因此x
似乎是問題所在。然而,x.reshape(-1,1)
幫助,nore x.reshape(1,-1)
。
你試圖重塑它的其他方式(1,-1)? –
是的,我已經試過了。看到我的評論約翰Moutafis答案。 –
重塑爲(-1,1)時,我沒有得到任何錯誤,在scikit 0.18 –