2017-04-13 64 views
0

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)

+2

你试图重塑它的其他方式(1,-1)? –

+0

是的,我已经试过了。看到我的评论约翰Moutafis答案。 –

+1

重塑为(-1,1)时,我没有得到任何错误,在scikit 0.18 –

回答

4

的名单,我发现了自己的错误。正如我在我的编辑中所述,不是fit()正在引发错误,而是score_samples()

这两个函数都会删除多维数组。

工作代码:

data = np.array(data).reshape(-1,1) 
clf = mixture.GaussianMixture(n_components=1, covariance_type='full') 
clf.fit(data) 

x = np.array(np.linspace(-8000,8000,32000)).reshape(-1,1) 
y = clf.score_samples(x) 

plt.plot(x, y) 
plt.show() 
+0

哦,你发现它:)正如我编辑我的答案! –

2

如果你只有一个功能的许多样品,试图

your_samples_list = map(lambda x:[x], your_samples_list) 

这将其转换成列表

[a,b,c] -> [[a],[b],[c]] 
相关问题