如何正确地将高斯混合模型拟合到单特征数据上?

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]

我想对这些数据进行高斯拟合并绘制它们。

如果我运行

我会得到错误

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

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注