Reputation: 336
I am trying to use GaussianMixture model for image segmetation, so I used 2 components, covariance matrix type="full" and tried to run using Spyder3.6 which comes with anaconda. Here's the code:
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])
But it shows ValueError and positive definite exception and I can't figure out why? Here's the trace of error.
`
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 definiteDuring 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.
`
Upvotes: 3
Views: 6095
Reputation: 61
I think the reason has already been presented in the ErrorInformation, which is "because some components have ill-defined empirical covariance (for instance caused by singleton or collapsed samples)". Since you set the number of components to 2, it can not be decreased, so I suggest you to increase the parameter "reg_covar" to 1e-5 (default to 1e-6).
more information about GMM's parameters can be found in: https://scikit-learn.org/stable/modules/generated/sklearn.mixture.GaussianMixture.html
Upvotes: 6