ImportError: cannot import name choice when importing sklearn.mixture

假装没事ソ 提交于 2019-12-19 18:27:48

问题


I am using scikit learn 0.15.0. When I try to import sklearn.mixture I get ImportError: cannot import name choice

Any ideas?

===================================================================

In [1]: **from sklearn import mixture**

ImportError                               Traceback (most recent call last)
<ipython-input-1-05bc76cab98d> in <module>()
----> 1 from sklearn import mixture

/home/f/anaconda/lib/python2.7/site-packages/sklearn/mixture/__init__.py in <module>()
      3 """
      4
----> 5 from .gmm import sample_gaussian, log_multivariate_normal_density
      6 from .gmm import GMM, distribute_covar_matrix_to_match_covariance_type
      7 from .gmm import _validate_covars

/home/f/anaconda/lib/python2.7/site-packages/sklearn/mixture/gmm.py in <module>()
     16 from ..utils import check_random_state, deprecated
     17 from ..utils.extmath import logsumexp, pinvh
---> 18 from .. import cluster
     19
     20 from sklearn.externals.six.moves import zip

/home/f/anaconda/lib/python2.7/site-packages/sklearn/cluster/__init__.py in <module>()
      4 """
      5
----> 6 from .spectral import spectral_clustering, SpectralClustering
      7 from .mean_shift_ import mean_shift, MeanShift, estimate_bandwidth, \
      8     get_bin_seeds

/home/f/anaconda/lib/python2.7/site-packages/sklearn/cluster/spectral.py in <module>()
     16 from ..neighbors import kneighbors_graph
     17 from ..manifold import spectral_embedding
---> 18 from .k_means_ import k_means
     19
     20

/home/f/anaconda/lib/python2.7/site-packages/sklearn/cluster/k_means_.py in <module>()
     28 from ..utils import as_float_array
     29 from ..utils import gen_batches
---> 30 from ..utils.random import choice
     31 from ..externals.joblib import Parallel
     32 from ..externals.joblib import delayed

ImportError: cannot import name choice

回答1:


As noted, it might have to do with old files (that are implemented in python as opposed to libraries?) in a new package you probably updated using pip install -U packagename (in my case pip install -U scikit-learn) It might be worth first trying to uninstall the package and install it again before manually deleting stuff... (worked in my case) uninstall (in my case run it on Anaconda Command prompt):

   pip uninstall scikit-learn

then:

   pip install -U numpy scipy scikit-learn

(added dependencies just in case) then just to be sure

   pip install scikit-learn

should say it's installed




回答2:


from sklearn.mixture import GaussianMixture

using this would make it more specific to work with .gmm,

and

from sklearn.cluster import KMeans

for:

     16 from ..neighbors import kneighbors_graph
     17 from ..manifold import spectral_embedding
---> 18 from .k_means_ import k_means



回答3:


I was getting the same error when I tried to import KMeans as : from sklearn.cluster import KMeans

Error > ImportError: cannot import name choice

I found the answer here: https://github.com/scikit-learn/scikit-learn/issues/3461

Since I have upgraded to Scikit version 0.15 a few days back, the older version of random.so was present in /usr/local/lib/python2.7/dist-packages/sklearn/utils.

I manually deleted it and now I do not get the error anymore.

Hope this helps.



来源:https://stackoverflow.com/questions/25001270/importerror-cannot-import-name-choice-when-importing-sklearn-mixture

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!