ImportError:没有名为sklearn.cross_validation的模块

时间:2015-06-05 13:15:18

标签: python scikit-learn

我在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

我需要做什么?

15 个答案:

答案 0 :(得分:218)

它必须与renaming and depreaction of cross_validation submodule to model_selection相关。尝试替换cross_validation - > model_selection

答案 1 :(得分:69)

train_test_split现在处于model_selection中。只需输入:

from sklearn.model_selection import train_test_split

它应该工作

答案 2 :(得分:25)

我想交叉选择不再有效。我们应该改用模型选择。您可以编写运行%c

就这样。

答案 3 :(得分:8)

可能是由于sklearn.cross_validation弃用。 请用sklearn.model_selection替换sklearn.cross_validation

参考- https://github.com/amueller/scipy_2015_sklearn_tutorial/issues/60

答案 4 :(得分:7)

sklearn.cross_validation

已更改为

sklearn.model_selection

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

答案 5 :(得分:6)

将数据集分为训练集和测试集

from sklearn.model_selection import train_test_split

答案 6 :(得分:5)

过去:from sklearn.cross_validation (此软件包从0.20开始在0.18版本中已弃用,已更改为from sklearn import model_selection。)

当前:from sklearn import model_selection

示例2:

过去:from sklearn.cross_validation import cross_val_score(不建议使用版本0.18)

当前:from sklearn.model_selection import cross_val_score

答案 7 :(得分:5)

sklearn.cross_validation现在更改为sklearn.model_selection

只是改变

sklearn.cross_validation

sklearn.model_selection

答案 8 :(得分:4)

如果您的代码需要运行各种版本,则可以执行以下操作:

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

这不是很理想,因为您将软件包版本作为字符串进行比较,通常可以,但并非总是如此。如果您愿意安装packaging,这是一种更好的方法:

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

答案 9 :(得分:3)

确保您已安装Anaconda,然后安装create a virtualenv using conda。这将确保所有导入工作

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

答案 10 :(得分:2)

train_test_split是模块sklearn.model_selection的一部分,因此,您可能需要从model_selection

导入模块

代码:

from sklearn.model_selection import train_test_split

答案 11 :(得分:1)

cross_validation早已过时,请尝试使用model_selection将其切换出

答案 12 :(得分:1)

像这样更改代码

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

答案 13 :(得分:0)

sklearn.cross_validation现在更改为sklearn.model_selection

只需使用

from sklearn.model_selection import train_test_split

我认为这会起作用。

答案 14 :(得分:0)

cross_validation 不再可用。

尝试使用 model_selection 而不是 cross_validation

from sklearn.model_selection import train_test_split