gpt4 book ai didi

python-3.x - 如何修复使用 sklearn.mixture.GaussianMixture 拟合 GMM 时的 ValueError?

转载 作者:行者123 更新时间:2023-12-05 02:17:06 35 4
gpt4 key购买 nike

我正在尝试使用 GaussianMixture 模型进行图像分割,所以我使用了 2 个组件,协方差矩阵 type="full"并尝试使用 anaconda 附带的 Spyder3.6 运行。这是代码:

from scipy.misc import imread, imshow
from sklearn.mixture import GaussianMixture as GMM
import graph_tool.all as gt
from graph_tool.all import *

X=imread('2.jpg')
old=X.shape
X=X.reshape(-1,3)
gmm=GMM(covariance_type='full', n_components=2)
gmm.fit(X)
clusters=gmm.predict(X)
clusters=clusters.reshape(old[0],old[1])

但它显示 ValueError 和正定异常,我不明白为什么?这是错误的踪迹。

`

Traceback (most recent call last):

File "/home/madhur/anaconda3/lib/python3.6/site-packages/sklearn/mixture/gaussian_mixture.py", line 318, in _compute_precision_cholesky cov_chol = linalg.cholesky(covariance, lower=True)
File "/home/madhur/anaconda3/lib/python3.6/site-packages/scipy/linalg/decomp_cholesky.py", line 81, in cholesky check_finite=check_finite)
File "/home/madhur/anaconda3/lib/python3.6/site-packages/scipy/linalg/decomp_cholesky.py", line 30, in _cholesky
raise LinAlgError("%d-th leading minor not positive definite" % info)
numpy.linalg.linalg.LinAlgError: 2-th leading minor not positive definite

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/madhur/Desktop/Project/graphcutmaterials/test.py", line 19, in gmm.fit(X)
File "/home/madhur/anaconda3/lib/python3.6/site-packages/sklearn/mixture/base.py", line 207, in fit self._initialize_parameters(X, random_state)
File "/home/madhur/anaconda3/lib/python3.6/site-packages/sklearn/mixture/base.py", line 157, in _initialize_parameters self._initialize(X, resp)
File "/home/madhur/anaconda3/lib/python3.6/site-packages/sklearn/mixture/gaussian_mixture.py", line 643, in _initialize covariances, self.covariance_type)
File "/home/madhur/anaconda3/lib/python3.6/site-packages/sklearn/mixture/gaussian_mixture.py", line 320, in _compute_precision_cholesky
raise ValueError(estimate_precision_error_message)

ValueError: Fitting the mixture model failed because some components have ill-defined empirical covariance (for instance caused by singleton or collapsed samples). Try to decrease the number of components, or increase reg_covar.

`

最佳答案

我认为原因已经在 ErrorInformation 中提出,这是“因为某些组件具有不明确的经验协方差(例如由单例或折叠样本引起)”。由于您将组件数设置为 2,因此无法减少,所以我建议您将参数“reg_covar”增加到 1e-5(默认为 1e-6)。

有关 GMM 参数的更多信息,请参阅:https://scikit-learn.org/stable/modules/generated/sklearn.mixture.GaussianMixture.html

关于python-3.x - 如何修复使用 sklearn.mixture.GaussianMixture 拟合 GMM 时的 ValueError?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48370066/

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