将高斯混合模型与单一特征数据进行匹配的正确方法是什么? [英] What is the correct way to fit a gaussian mixture model to single feature data?
本文介绍了将高斯混合模型与单一特征数据进行匹配的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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]
我想将一些高斯型数据与此数据匹配,并将它们绘制成曲线图。
如果我运行
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)
和x.reshape(1,-1)
都没有帮助。
推荐答案
我自己找到了错误。正如我在编辑中所述,不是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()
这篇关于将高斯混合模型与单一特征数据进行匹配的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文