gpt4 book ai didi

python - 高斯混合模型 _ Scikit Learn _ 如何拟合单 D 数据?

转载 作者:行者123 更新时间:2023-11-30 09:43:27 24 4
gpt4 key购买 nike

我已经开始在 Sklearn 库中使用 GMM。我有如下所示的一维数据

np.random.seed(2)
x = np.concatenate([np.random.normal(0, 2, 2000),
np.random.normal(5, 5, 2000),
np.random.normal(3, 0.5, 600)])

我想使用sklearn GaussainMixture函数来拟合4高斯混合。所以我尝试了

clf= GaussianMixture(n_components = 4, max_iter=500, random_state=3).fit(x)

问题

当我运行上面的代码时,我收到一个错误

Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

我的回溯是

Traceback (most recent call last):
File "C:\Users\VW3ZTWS\PycharmProjects\Data_Collection_and_learnings\venv\lib\site-packages\IPython\core\interactiveshell.py", line 2869, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-44-7de666249812>", line 1, in <module>
clf= GaussianMixture(n_components = 4, max_iter=500, random_state=3).fit(x)
File "C:\Users\VW3ZTWS\PycharmProjects\Data_Collection_and_learnings\venv\lib\site-packages\sklearn\mixture\base.py", line 194, in fit
self.fit_predict(X, y)
File "C:\Users\VW3ZTWS\PycharmProjects\Data_Collection_and_learnings\venv\lib\site-packages\sklearn\mixture\base.py", line 220, in fit_predict
X = _check_X(X, self.n_components, ensure_min_samples=2)
File "C:\Users\VW3ZTWS\PycharmProjects\Data_Collection_and_learnings\venv\lib\site-packages\sklearn\mixture\base.py", line 55, in _check_X
ensure_min_samples=ensure_min_samples)
File "C:\Users\VW3ZTWS\PycharmProjects\Data_Collection_and_learnings\venv\lib\site-packages\sklearn\utils\validation.py", line 552, in check_array
"if it contains a single sample.".format(array))
ValueError: Expected 2D array, got 1D array instead:
array=[-0.03338572 0.3163226 -1.94596018 ... 2.93448979 2.77931282
3.28590084].

问题

我是否无法将 GMM 拟合为一维数据?我不确定我犯了什么错误,请澄清 -

最佳答案

您发布的内容告诉您如何继续:

Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

如果您的数据集是一维的,它只有一个特征,因此:

x = x.reshape(-1, 1)

其余代码应该可以工作。

关于python - 高斯混合模型 _ Scikit Learn _ 如何拟合单 D 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55919914/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com