【问题标题】:sklearn import returning an errorsklearn 导入返回错误
【发布时间】:2019-02-01 23:44:27
【问题描述】:

我是机器学习和 sklearn 包的新手。尝试导入 sklearn 时,我收到一条错误消息,提示它找不到 DLL。我通过 pip 安装了 sklearn,卸载了包括 python 在内的所有内容并重新安装了所有内容,但仍然遇到同样的问题。这台机器上只安装了一个版本的python。我正在运行 python 3.6.1 并且还安装了 Visual Studio 2017 社区。所有软件包都是最新的。回溯如下。 (从所有路径中删除用户名)

正在运行的代码:

import numpy as np
from sklearn import cross_validation, neighbors
import pandas as pd

Traceback (most recent call last):
  File "C:/Users/Public/Documents/Machine learning project/Classification/KNN.py", line 2, in <module>
    from sklearn import cross_validation, neighbors
  File "C:\Users\\AppData\Roaming\Python\Python36\site-packages\sklearn\__init__.py", line 134, in <module>
    from .base import clone
  File "C:\Users\\AppData\Roaming\Python\Python36\site-packages\sklearn\base.py", line 11, in <module>
    from scipy import sparse
  File "C:\Users\\AppData\Roaming\Python\Python36\site-packages\scipy\sparse\__init__.py", line 229, in <module>
    from .csr import *
  File "C:\Users\\AppData\Roaming\Python\Python36\site-packages\scipy\sparse\csr.py", line 15, in <module>
    from ._sparsetools import csr_tocsc, csr_tobsr, csr_count_blocks, get_csr_submatrix, csr_sample_values
ImportError: DLL load failed: %1 is not a valid Win32 application.

【问题讨论】:

  • 更新你的 scipy
  • 使用 64 位版本的 python,我认为使用 PIP 会安装正确版本的包。但至少在我的情况下,它似乎没有。我现在已经编译了二进制文件并以这种方式安装,世界上一切正常!感谢数百万洛根
  • 检查你的点子列表

标签: python python-3.x scikit-learn


【解决方案1】:

检查您正在使用的 python 版本。是64位还是32位?我唯一一次看到这个错误是包类型和 Python 版本不匹配。

如果没有问题,您可以尝试以下方法:

import imp
imp.find_module("sklearn")

这将准确告诉您正在加载的内容以及加载路径。

如果这是加载正确的包,我会说尝试手动安装包二进制文件,而不是通过 pip。但是我只是对其进行了测试,发现它在我的系统上运行。

【讨论】:

    【解决方案2】:

    我完全同意使用 from sklearn.model_selection import cross_validate,但是当您尝试训练数据集时该过程失败。我宁愿推荐导入以下库,因为您的目标是执行 train_test_split 功能:

    from sklearn.model_selection import train_test_split as tts   
    ...
    ...
    ...
    train_feats, test_feats, train_labels, test_labels = tts(features, labels, test_size=0.2)
    

    【讨论】:

      【解决方案3】:

      虽然根据提供的内容很难猜出您遇到的问题,但请尝试以下操作:

      from sklearn.model_selection import cross_validate
      from sklearn.neighbors import KNeighborsClassifier
      

      【讨论】:

        猜你喜欢
        • 2017-05-16
        • 2017-05-04
        • 2016-10-03
        • 2018-05-20
        • 2019-07-11
        • 2019-06-26
        • 1970-01-01
        • 2019-05-19
        • 1970-01-01
        相关资源
        最近更新 更多