没有名为sklearn.cross_validation的模块

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

我需要做什么?

450647 次浏览

确保已经安装了水蟒,然后安装了使用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

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

可能是因为sklearn.cross_validation的弃用。 请替换sklearn。sklearn.model_selection

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

Train_test_split现在在model_selection中。类型:

from sklearn.model_selection import train_test_split

应该可以

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

from sklearn.model_selection import train_test_split

我猜交叉选择已经不活跃了。我们应该用模型选择来代替。你可以写它来运行,from sklearn.model_selection import train_test_split

这是它。

Past: from sklearn.cross_validation (此包在0.18版本中已弃用,从0.20起改为from sklearn import model_selection)

礼物:from sklearn import model_selection

示例2:

过去:from sklearn.cross_validation import cross_val_score(版本0.18已弃用)

Present: from sklearn.model_selection import cross_val_score

sklearn.cross_validation

已改为

sklearn.model_selection

在这里检查文档: https://scikit-learn.org/stable/modules/cross_validation.html < / p >

sklearn.cross_validation现在改为sklearn.model_selection

只使用

from sklearn.model_selection import train_test_split

我想那会有用的。

Cross_validation在前一段时间已被弃用,请尝试使用model_selection将其替换掉

sklearn.cross_validation现在改为sklearn.model_selection

只改变

sklearn.cross_validation

sklearn.model_selection

像这样更改代码

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

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

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

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

代码:

from sklearn.model_selection import train_test_split

cross_validation不再可用。

尝试使用model_selection而不是cross_validation:

from sklearn.model_selection import train_test_split