Problem with GMM library from sklear.mixture?
Question:
I want to run this example about 1D Gaussian Mixture Example: http://www.astroml.org/book_figures/chapter4/fig_GMM_1D.html
But I have this error all the time:
from sklearn.mixture import GMM
ImportError: cannot import name 'GMM'
I tried to replace it by from sklearn.mixture import GaussianMixture
but the code does not work, they do not have the same functionalities.
Thank you in advance.
Answers:
The newer versions of scikit-learn don’t have that module. From looking at the versions it was deprecated in v 0.18 and removed in v 0.20. Here is the link to the OLD 0.18 module, which is the first instance i could find that shows a deprecation warning. https://scikit-learn.org/0.18/modules/generated/sklearn.mixture.GMM.html#sklearn.mixture.GMM if you want you can install an older version
pip install -Iv scikit-learn==0.15
, or if you want to use the newer version, adapt the parameters of your GaussianMixture to reflect their new names (e.g. in GaussianMixture, max_iter
is the number of iterations, instead of n_iter
).
sklearn Gaussian Mixture implementation
Old (outdated, not supported in newer sklearn versions):
from sklearn.mixture import GMM
model = GMM(n_components=3,covariance_type='full')
New and supported
from sklearn import mixture
model = mixture.GaussianMixture(n_components=3, covariance_type='full')
n_components
default value is 1, choose what you want. That’s number of mixture components.
Use this instead
from sklearn.mixture import GaussianMixture as GMM
model=GMM(n_components=3,covariance_type='full')
model.fit(X_iris)
This should solve your issue
I want to run this example about 1D Gaussian Mixture Example: http://www.astroml.org/book_figures/chapter4/fig_GMM_1D.html
But I have this error all the time:
from sklearn.mixture import GMM
ImportError: cannot import name 'GMM'
I tried to replace it by from sklearn.mixture import GaussianMixture
but the code does not work, they do not have the same functionalities.
Thank you in advance.
The newer versions of scikit-learn don’t have that module. From looking at the versions it was deprecated in v 0.18 and removed in v 0.20. Here is the link to the OLD 0.18 module, which is the first instance i could find that shows a deprecation warning. https://scikit-learn.org/0.18/modules/generated/sklearn.mixture.GMM.html#sklearn.mixture.GMM if you want you can install an older version
pip install -Iv scikit-learn==0.15
, or if you want to use the newer version, adapt the parameters of your GaussianMixture to reflect their new names (e.g. in GaussianMixture, max_iter
is the number of iterations, instead of n_iter
).
sklearn Gaussian Mixture implementation
Old (outdated, not supported in newer sklearn versions):
from sklearn.mixture import GMM
model = GMM(n_components=3,covariance_type='full')
New and supported
from sklearn import mixture
model = mixture.GaussianMixture(n_components=3, covariance_type='full')
n_components
default value is 1, choose what you want. That’s number of mixture components.
Use this instead
from sklearn.mixture import GaussianMixture as GMM
model=GMM(n_components=3,covariance_type='full')
model.fit(X_iris)
This should solve your issue