mac OS X10.11.5安装scikit-learn后运行出现ValueError: numpy.dtype has the wrong size的错误

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/stark_summer/article/details/51980888
  1. 安装机器学习类库
    通过pip命令安装机器学习类库 numpy,scipy,scikit-learn

    sudo pip install numpy scipy scikit-learn 

    但是安装后,运行机器学习算法,报错如下:

    Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Library/Python/2.7/site-packages/sklearn/__init__.py", line 57, in <module> from .base import clone File "/Library/Python/2.7/site-packages/sklearn/base.py", line 11, in <module> from .utils.fixes import signature File "/Library/Python/2.7/site-packages/sklearn/utils/__init__.py", line 10, in <module> from .murmurhash import murmurhash3_32 File "numpy.pxd", line 155, in init sklearn.utils.murmurhash (sklearn/utils/murmurhash.c:5029) ValueError: numpy.dtype has the wrong size, try recompiling 
  2. 排查错误原因

    网上给的建议大都是直接卸载再全部重装,将 numpy 、 scipy 和 scikit-learn 全部卸载了,然后在安装,操作如下
    我在操作时候发现,不能通过简单的安装和卸载方式重新安装,因为重新安装可能某些类库仍然是低版本,所以要选择升级,这样能保证版本都是最新的

    sudo pip install -U numpy scipy scikit-learn 

    安装在执行这个命令时候,会报错如下:

    Exception:
    Traceback (most recent call last):
      File "/Library/Python/2.7/site-packages/pip-7.1.2-py2.7.egg/pip/basecommand.py", line 211, in main
        status = self.run(options, args)
      File "/Library/Python/2.7/site-packages/pip-7.1.2-py2.7.egg/pip/commands/install.py", line 311, in run
        root=options.root_path,
      File "/Library/Python/2.7/site-packages/pip-7.1.2-py2.7.egg/pip/req/req_set.py", line 640, in install
        requirement.uninstall(auto_confirm=True)
      File "/Library/Python/2.7/site-packages/pip-7.1.2-py2.7.egg/pip/req/req_install.py", line 716, in uninstall
        paths_to_remove.remove(auto_confirm)
      File "/Library/Python/2.7/site-packages/pip-7.1.2-py2.7.egg/pip/req/req_uninstall.py", line 125, in remove
        renames(path, new_path)
      File "/Library/Python/2.7/site-packages/pip-7.1.2-py2.7.egg/pip/utils/__init__.py", line 315, in renames
        shutil.move(old, new)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 302, in move
        copy2(src, real_dst)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 131, in copy2
        copystat(src, dst)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 103, in copystat
        os.chflags(dst, st.st_flags)
    OSError: [Errno 1] Operation not permitted: 
    

  3. 继续剖析错误原因
    查了资料,找到如下解决方案:
    1、重启电脑,按command+R进入恢复模式,点击菜单【实用工具】,打开【终端】,输入 csrutil disable
    2、重启电脑,正常进入,打开【终端】输入 csrutil status

    这是macosx 10.11 EI Capitan使用了Rootlees,可以理解为一个更高等级的内核保护,系统会默认锁定/system , /sbin , /usr这三个目录,

    通过上面方法可以将这个保护关闭。

    如果没有关闭,安装一些软件时会出现

    Operation not permitted

    重新执行如下命令:

    sudo pip install -U numpy scipy scikit-learn 

    可以正常执行

    但是重新运行机器学习算法,还是报上面的错


  4. 尝试新思路解决问题

    继续查资料时终于发现有用的方法了:

    http://scikit-learn-general.narkive.com/kMA6mRCk/valueerror-numpy-dtype-has-the-wrong-size-try-recompiling

    就是不用使用 pip install scikit-learn 安装,卸载之后直接使用git上 https://github.com/scikit-learn/scikit-learn 的自己安装
    操作流程如下:

    sudo pip uninstall scikit-learn 
    
    git clone https://github.com/scikit-learn/scikit-learn.git
    
    cd scikit-learn
    sudo make
    执行make会报错如下:
    RuntimeError: Running cythonize failed! 
    Error提示,这时候安装一下 cpython 即可
    sudo pip install cython
    
    然后 继续执行make
    最后执行install
    sudo python setup.py install
    
    copying sklearn/datasets/data/digits.csv.gz -> /Library/Python/2.7/site-packages/sklearn/datasets/data/
    copying sklearn/datasets/data/linnerud_physiological.csv -> /Library/Python/2.7/site-packages/sklearn/datasets/data/
    copying sklearn/datasets/data/breast_cancer.csv -> /Library/Python/2.7/site-packages/sklearn/datasets/data/
    copying sklearn/datasets/data/linnerud_exercise.csv -> /Library/Python/2.7/site-packages/sklearn/datasets/data/
    creating /Library/Python/2.7/site-packages/sklearn/datasets/images
    copying sklearn/datasets/images/flower.jpg -> /Library/Python/2.7/site-packages/sklearn/datasets/images/
    copying sklearn/datasets/images/README.txt -> /Library/Python/2.7/site-packages/sklearn/datasets/images/
    copying sklearn/datasets/images/china.jpg -> /Library/Python/2.7/site-packages/sklearn/datasets/images/
    creating /Library/Python/2.7/site-packages/sklearn/datasets/descr
    copying sklearn/datasets/descr/digits.rst -> /Library/Python/2.7/site-packages/sklearn/datasets/descr/
    copying sklearn/datasets/descr/linnerud.rst -> /Library/Python/2.7/site-packages/sklearn/datasets/descr/
    copying sklearn/datasets/descr/breast_cancer.rst -> /Library/Python/2.7/site-packages/sklearn/datasets/descr/
    copying sklearn/datasets/descr/iris.rst -> /Library/Python/2.7/site-packages/sklearn/datasets/descr/
    copying sklearn/datasets/descr/boston_house_prices.rst -> /Library/Python/2.7/site-packages/sklearn/datasets/descr/
    copying sklearn/datasets/descr/diabetes.rst -> /Library/Python/2.7/site-packages/sklearn/datasets/descr/
    running install_egg_info
    Writing /Library/Python/2.7/site-packages/scikit_learn-0.18.dev0-py2.7.egg-info
    running install_clib

    “running install_clib” 表示顺利安装成功

    再次运行机器学习算法,不再报错了

经常在自己mac运行下机器学习算法,记得之前就遇到过这样问题,这次正好又遇到了,就整理下, 希望能对大家有所帮助吧,也不是啥干货,就是简单整理一下。


作者:stark_summer
出处:http://blog.csdn.net/stark_summer/article/details/51980888

猜你喜欢

转载自blog.csdn.net/stark_summer/article/details/51980888