Python 导入错误:导入 sklearn.mixture 时无法导入名称选择

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25001270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:32:07  来源:igfitidea点击:

ImportError: cannot import name choice when importing sklearn.mixture

pythonscikit-learnpython-import

提问by user1487551

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

我正在使用 scikit 学习 0.15.0。当我尝试导入 sklearn.mixture 时,出现 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

回答by skv

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

当我尝试这样import KMeans做时,我遇到了同样的错误:from sklearn.cluster import KMeans

Error > ImportError: cannot import name choice

Error > ImportError: cannot import name choice

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

我在这里找到了答案: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 ofrandom.so was present in/usr/local/lib/python2.7/dist-packages/sklearn/utils.

由于几天前我升级到 Scikit 0.15 版,旧版本的random.so 出现在/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.

希望这可以帮助。

回答by ntg

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):

如前所述,它可能与旧文件(在 python 中实现而不是库中实现?)有关,您可能使用 pip install -U packagename 更新了新包(在我的情况下是 pip install -U scikit-learn)它可能值得首先尝试卸载软件包并在手动删除内容之前再次安装它......(在我的情况下工作)卸载(在我的情况下在Anaconda命令提示符下运行它):

   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

应该说它已安装

回答by Fareesa

from sklearn.mixture import GaussianMixture

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

使用它可以更具体地使用 .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