我使用的是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

我需要做什么?


当前回答

sklearn.cross_validation

已改为

sklearn.model_selection

在这里查看文档: https://scikit-learn.org/stable/modules/cross_validation.html

其他回答

sklearn.cross_validation

已改为

sklearn.model_selection

在这里查看文档: https://scikit-learn.org/stable/modules/cross_validation.html

如果你有需要运行不同版本的代码,你可以这样做:

import sklearn
if sklearn.__version__ > '0.18':
    from sklearn.model_selection import train_test_split
else:
    from sklearn.cross_validation import train_test_split

但这并不理想,因为您将包版本作为字符串进行比较,这通常有效,但并不总是如此。如果你愿意安装打包,这是一个更好的方法:

from packaging.version import parse
import sklearn
if parse(sklearn.__version__) > parse('0.18'):
    from sklearn.model_selection import train_test_split
else:
    from sklearn.cross_validation import train_test_split

cross_validation不再可用。

尝试使用model_selection代替cross_validation:

from sklearn.model_selection import train_test_split

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

这可能是由于sklearn.cross_validation的弃用。 请替换sklearn。使用sklearn.model_selection进行交叉验证

Ref - https://github.com/amueller/scipy_2015_sklearn_tutorial/issues/60