我已经开始在 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)
问题
当我运行上面的代码时,我收到一个错误
如果您的数据具有单个特征,则使用 array.reshape(-1, 1) 重塑您的数据,如果它包含单个样本,则使用 array.reshape(1, -1) 。
我的追溯是
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?我不确定我犯了什么错误,请澄清-