Cannot import Scikit-Learn

2019-01-15 02:03发布

问题:

I tried to install scikit-learn on my Linux Mint 12 but failed. I downloaded the package from http://pypi.python.org/pypi/scikit-learn/ and installed with

sudo python2.7 setup.py install

I then changed the directory to home and started python2.7 shell. On importing sklearn I got:

>>> import sklearn
/usr/lib/python2.7/dist-packages/scipy/spatial/__init__.py:7: RuntimeWarning: 
numpy.dtype size changed, may indicate binary incompatibility
    from ckdtree import *
/usr/lib/python2.7/dist-packages/scipy/spatial/__init__.py:7: RuntimeWarning:
numpy.ndarray size changed, may indicate binary incompatibility
    from ckdtree import *
/usr/lib/python2.7/dist-packages/scipy/spatial/__init__.py:7: RuntimeWarning: 
numpy.ufunc size changed, may indicate binary incompatibility
    from ckdtree import *
/usr/lib/python2.7/dist-packages/scipy/spatial/__init__.py:8: RuntimeWarning: 
numpy.dtype size changed, may indicate binary incompatibility
    from qhull import *
/usr/lib/python2.7/dist-packages/scipy/spatial/__init__.py:8: RuntimeWarning: 
numpy.ndarray size changed, may indicate binary incompatibility
    from qhull import *
/usr/lib/python2.7/dist-packages/scipy/spatial/__init__.py:8: RuntimeWarning:  
numpy.ufunc size changed, may indicate binary incompatibility
    from qhull import *

I think the problem is with scipy's spatial. This is because when I do

>>> from scipy import spatial

I get the same error that I get for Scikit-learn.

Please help. Thank You.

EDIT: New error.

>>> import sklearn
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/scikit_learn-0.12_git-py2.7-linux-
x86_64.egg/sklearn/__init__.py", line 17, in <module>
    from .base import clone
  File "/usr/local/lib/python2.7/dist-packages/scikit_learn-0.12_git-py2.7-linux-
x86_64.egg/sklearn/base.py", line 11, in <module>
    from .metrics import r2_score
  File "/usr/local/lib/python2.7/dist-packages/scikit_learn-0.12_git-py2.7-linux-
x86_64.egg/sklearn/metrics/__init__.py", line 6, in <module>
    from .metrics import confusion_matrix, roc_curve, auc, precision_score, \
  File "/usr/local/lib/python2.7/dist-packages/scikit_learn-0.12_git-py2.7-linux-  
x86_64.egg/sklearn/metrics/metrics.py", line 17, in <module>
    from ..utils import check_arrays
  File "/usr/local/lib/python2.7/dist-packages/scikit_learn-0.12_git-py2.7-linux-
x86_64.egg/sklearn/utils/__init__.py", line 9, in <module>
    from .murmurhash import murmurhash3_32
  File "numpy.pxd", line 174, in init sklearn.utils.murmurhash (sklearn/utils
/murmurhash.c:4776)
ValueError: numpy.ndarray has the wrong size, try recompiling

回答1:

I'm not really sure, but according to this that error happens when a package that depends on numpy is compiled targeting a specific version (or a specific platform), and that package is then installed in a different environment. How did you install those two packages? (numpy and scipy)

I'd suggest uninstalling both and then reinstalling SciPy, either using pip or from source.



回答2:

I ran into this today on OS X El Capitan. Unfortunately, reinstalling/installing didn't work, since the problem ended up being that numpy installation native to OS X was preferred over the one being installed by pip.

I was able to solve this by following the guidance in: https://apple.stackexchange.com/a/223163

In particular, I used the approach of fixing the python path to prefer pip libraries over OS X native libraries:

Create a file called fix_mac_path.pth in /Library/Python/2.7/site-packages that has the following:

import sys; std_paths=[p for p in sys.path if p.startswith('/System/')]; sys.path=[p for p in sys.path if not p.startswith('/System/')]+std_paths



回答3:

i was having the exact same problem. I managed to make it work by installing the windows binary for 64 bit windows from http://www.lfd.uci.edu/~gohlke/pythonlibs/#scikit-learn . Hope this works for you