Python 导入 scikit-learn 模块时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30766274/
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-19 08:56:25  来源:igfitidea点击:

Error importing scikit-learn modules

pythonscikit-learn

提问by Jon Rein

I'm trying to call a function from the cluster module, like so:

我正在尝试从集群模块调用一个函数,如下所示:

import sklearn
db = sklearn.cluster.DBSCAN()

and I get the following error:

我收到以下错误:

AttributeError: 'module' object has no attribute 'cluster'

Tab-completing in IPython, I seem to have access to the base, clone, externals, re, setup_module, sys, and warning modules. Nothing else, though others (including cluster) are in the sklearn directory.

在 IPython 中完成选项卡,我似乎可以访问 base、clone、externals、re、setup_module、sys 和 warning 模块。没有别的,尽管其他(包括集群)在 sklearn 目录中。

Following pbu's advice below and using

遵循以下 pbu 的建议并使用

from sklearn import cluster

I get:

我得到:

Traceback (most recent call last):
  File "test.py", line 2, in <module>
    from sklearn import cluster
  File "C:\Python34\lib\site-packages\sklearn\cluster\__init__.py", line 6, in <module>
    from .spectral import spectral_clustering, SpectralClustering
  File "C:\Python34\lib\site-packages\sklearn\cluster\spectral.py", line 13, in <module>
    from ..utils import check_random_state, as_float_array
  File "C:\Python34\lib\site-packages\sklearn\utils\__init__.py", line 16, in <module>
    from .class_weight import compute_class_weight, compute_sample_weight
  File "C:\Python34\lib\site-packages\sklearn\utils\class_weight.py", line 7, in <module>
    from ..utils.fixes import in1d
  File "C:\Python34\lib\site-packages\sklearn\utils\fixes.py", line 318, in <module>
    from scipy.sparse.linalg import lsqr as sparse_lsqr
  File "C:\Python34\lib\site-packages\scipy\sparse\linalg\__init__.py", line 109, in <module>
    from .isolve import *
  File "C:\Python34\lib\site-packages\scipy\sparse\linalg\isolve\__init__.py", line 6, in <module>
    from .iterative import *
  File "C:\Python34\lib\site-packages\scipy\sparse\linalg\isolve\iterative.py", line 7, in <module>
    from . import _iterative
ImportError: DLL load failed: The specified module could not be found.

I'm using Python 3.4 on Windows, scikit-learn 0.16.1.

我在 Windows 上使用 Python 3.4,scikit-learn 0.16.1。

采纳答案by Jon Rein

Problem was with scipy/numpy install. I'd been using the (normally excellent!) unofficial installers from http://www.lfd.uci.edu/~gohlke/pythonlibs/. Uninstall/re-install from there made no difference, but installing with the official installers (linked from http://www.scipy.org/install.html) did the trick.

问题在于 scipy/numpy 安装。我一直在使用来自http://www.lfd.uci.edu/~gohlke/pythonlibs/的(通常非常好!)非官方安装程序。从那里卸载/重新安装没有任何区别,但使用官方安装程序(从http://www.scipy.org/install.html链接)进行安装可以解决问题。

回答by pbu

Place this line on top of the python file

将此行放在 python 文件的顶部

from sklearn import cluster

That should do it :))

应该这样做:))

回答by Jirka

You probably don't use Numpy+MKL, but only Numpy.

您可能不会使用 Numpy+MKL,而只会使用 Numpy。

I had the same problem and reinstalling Numpy with MKL

我遇到了同样的问题并用 MKL 重新安装了 Numpy

pip install --upgrade --force-reinstall "numpy?1.16.3+mkl?cp37?cp37m?win32.whl"

pip install --upgrade --force-reinstall "numpy?1.16.3+mkl?cp37?cp37m?win32.whl"

fixed it.

修复。

Note: update the file to the latest version, possibly 64bit - see the list of available Windows binaries

注意:将文件更新到最新版本,可能是 64 位 - 请参阅可用的 Windows 二进制文件列表

回答by Ben

I am using anacondagot the same error as the OP, when loading Orange, or PlotNine. I can't recall when this start to happen.

anaconda在加载 Orange 或 PlotNine 时,我使用的错误与 OP 相同。我不记得这是什么时候开始发生的。

Tracing the dependency of Anaconda3\Lib\site-packages\scipy\special\_ufuncs.cp36-win32.pyd, libifcoremd.dlland libmmd.dllare missing in DependencyWalk. Searching them in anaconda root directry, they are located in both ICC_RT and one version of MKL package.

跟踪Anaconda3\Lib\site-packages\scipy\special\_ufuncs.cp36-win32.pydlibifcoremd.dll和的依赖关系libmmd.dll在 DependencyWalk 中缺失。在 anaconda 根目录中搜索它们,它们位于 ICC_RT 和一个版本的 MKL 包中。

Adding Anaconda3\pkgs\mkl-2017.0.3-0\Library\binto PATH, seems to fix SciPy and NumPy related DLL load failure, the above package starts to work again.

添加Anaconda3\pkgs\mkl-2017.0.3-0\Library\bin到PATH,似乎修复了SciPy和NumPy相关的DLL加载失败,上面的包又开始工作了。

I still don't know how to fix this properly. Apparently the downside is that the MKL package could be updated and versions may change so does the path. In this aspect Its equally inconvenient as adding a non-managed package.

我仍然不知道如何正确解决这个问题。显然,缺点是 MKL 包可能会更新,版本可能会更改,路径也会更改。在这方面,它与添加非托管包同样不方便。

Reinstalling ICC_RT fixed the issue for me, libmmd.dlland the related dlls are automatically copied into anaconda3/library/binafterwards, which is automatically added into PATHby activatecommand. All previous numpy/scipy related cant load DLLerrors are gone now.

重新安装ICC_RT 解决了我的问题,后面libmmd.dll会自动复制相关的dll anaconda3/library/binPATH通过activate命令自动添加进去。所有以前的 numpy/scipy 相关cant load DLL错误现在都消失了。

回答by Tin Luu

From the error log, it shows that scipy moduleis the most recent module fails to import

从错误日志中可以看出,scipy 模块是最近导入失败的模块

  File "C:\Python34\lib\site-packages\sklearn\utils\fixes.py", line 318, in <module>
    from scipy.sparse.linalg import lsqr as sparse_lsqr
  File "C:\Python34\lib\site-packages\scipy\sparse\linalg\__init__.py", line 109, in <module>
    from .isolve import *
  File "C:\Python34\lib\site-packages\scipy\sparse\linalg\isolve\__init__.py", line 6, in <module>
    from .iterative import *
  File "C:\Python34\lib\site-packages\scipy\sparse\linalg\isolve\iterative.py", line 7, in <module>
    from . import _iterative
ImportError: DLL load failed: The specified module could not be found.

I have the same error that show the same log, the problem'd gone when I uninstall/install scipy:

我有同样的错误显示相同的日志,当我卸载/安装 scipy 时问题就消失了:

pip uninstall scipy
pip install scipy

回答by Saeid Bagheri

For me what fixed it were these commands:

对我来说,修复它的是这些命令:

pip uninstall sklearn
pip uninstall scikit-learn
pip uninstall scipy
pip install scipy
pip install scikit-learnhere

回答by Rene B.

I had the same issue and solved it by installing/updating the mklpackage:

我遇到了同样的问题并通过安装/更新mkl包解决了它:

conda install mkl

or

或者

pip install mkl

Just for full information, this also downgraded the following packages:

仅供参考,这也降级了以下软件包:

The following packages will be UPDATED:

以下软件包将被更新:

mkl:          2017.0.4-h6d528fc_0 defaults --> 2018.0.3-1         defaults

The following packages will be DOWNGRADED:

以下软件包将被降级:

numpy:        1.11.3-py34_0       defaults --> 1.10.1-py34_0      defaults
pandas:       0.19.2-np111py34_1  defaults --> 0.18.1-np110py34_0 defaults
scikit-learn: 0.18.1-np111py34_1  defaults --> 0.17-np110py34_1   defaults
scipy:        0.19.1-np111py34_0  defaults --> 0.16.0-np110py34_0 defaults

回答by Sumanth Lazarus

I struggled trying to figure this one out; tried to download and install the (unofficial) Numpy+MKL library from the website (risky/tedious?).

我努力想弄清楚这个问题;尝试从网站下载并安装(非官方)Numpy+MKL 库(有风险/乏味?)。

Ultimately found success by:

最终通过以下方式获得成功:

  1. Login to command prompt using admin rights; how to here: https://superuser.com/questions/968214/open-cmd-as-admin-with-windowsr-shortcut

  2. Uninstall existing/tangled version of Scipy & Numpy pip uninstall scipy pip uninstall numpy

  3. Fresh install Scipy & Numpy pip install scipy pip install numpy

  4. Run Jupyter notebook; it worked for me.
  1. 使用管理员权限登录到命令提示符;如何在这里:https: //superuser.com/questions/968214/open-cmd-as-admin-with-windowsr-shortcut

  2. 卸载 Scipy 和 Numpy 的现有/纠结版本 pip uninstall scipy pip uninstall numpy

  3. 全新安装 Scipy 和 Numpy pip install scipy pip install numpy

  4. 运行 Jupyter 笔记本;它对我有用。

The message ImportError: DLL load failed: The specified module could not be foundinforms that there is failure to identify and source the required DLL(s) to use the scikit-learn library; a fresh install of scipy/numpy probably enables a better routing of DLL connections called from Jupyter notebook code(s).

消息ImportError: DLL load failed: The specified module could not be found通知无法识别和获取使用 scikit-learn 库所需的 DLL;全新安装的 scipy/numpy 可能可以更好地路由从 Jupyter 笔记本代码调用的 DLL 连接。

回答by Niwin

download microsoft visual c++ distribution

下载 Microsoft Visual C++ 发行版

link : https://www.microsoft.com/en-in/download/details.aspx?id=53840

链接:https: //www.microsoft.com/en-in/download/details.aspx?id=53840

vc_redist.x64.exe

vc_redist.x64.exe

install and run this .exe file in your computer.. the DLL import module error will not appear after this

在您的计算机中安装并运行此 .exe 文件。此后不会出现 DLL 导入模块错误

now it will work fine enjoy :)

现在它会工作正常享受:)