【问题标题】:What is the correct way to fit a gaussian mixture model to single feature data?将高斯混合模型拟合到单个特征数据的正确方法是什么?
【发布时间】:2017-09-09 05:38:42
【问题描述】:

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) 都没有帮助。

【问题讨论】:

  • 您是否尝试过以另一种方式重塑它 (1, -1)?
  • 是的,我已经尝试过了。请参阅我对 John Moutafis 回答的评论。
  • 在 scikit 0.18 上重塑为 (-1,1) 时我没有收到任何错误

标签: python machine-learning scikit-learn reshape mixture-model


【解决方案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()

【讨论】:

    【解决方案2】:

    如果您有很多只针对一个功能的示例,请尝试

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

    这会将其转换为列表列表

    [a,b,c] -> [[a],[b],[c]]
    

    【讨论】:

      猜你喜欢
      • 2017-02-26
      • 2019-02-25
      • 2018-11-09
      • 1970-01-01
      • 2020-08-12
      • 2018-07-19
      • 2015-11-23
      • 2014-08-02
      • 2012-03-04
      相关资源
      最近更新 更多