我使用的是Ubuntu 14.04中的python 2.7。我用这些命令安装了scikit-learn, numpy和matplotlib:

sudo apt-get install build-essential python-dev python-numpy \
python-numpy-dev python-scipy libatlas-dev g++ python-matplotlib \
ipython

但是当我导入这些包时:

from sklearn.cross_validation import train_test_split

它返回给我这个错误:

ImportError: No module named sklearn.cross_validation

我需要做什么?


当前回答

像这样更改代码

# from sklearn.cross_validation import train_test_split
from sklearn.model_selection import train_test_split

其他回答

cross_validation不再可用。

尝试使用model_selection代替cross_validation:

from sklearn.model_selection import train_test_split

Train_test_split是sklearn模块的一部分。因此,您可能需要从Model_selection导入模块

代码:

from sklearn.model_selection import train_test_split

它必须与model_selection的cross_validation子模块的重命名和弃用有关。尝试将cross_validation替换为model_selection

像这样更改代码

# from sklearn.cross_validation import train_test_split
from sklearn.model_selection import train_test_split

确保你已经安装了Anaconda,然后使用conda创建一个virtualenv。这将确保所有导入工作正常

Python 2.7.9 |Anaconda 2.2.0 (64-bit)| (default, Mar  9 2015, 16:20:48) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://binstar.org
>>> from sklearn.cross_validation import train_test_split