Python 导入错误:没有名为 sklearn.cross_validation 的模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30667525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:47:19  来源:igfitidea点击:

ImportError: No module named sklearn.cross_validation

pythonscikit-learn

提问by arthurckl

I am using python 2.7 in Ubuntu 14.04. I installed scikit-learn, numpy and matplotlib with these commands:

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

But when I import these packages:

但是当我导入这些包时:

from sklearn.cross_validation import train_test_split

It returns me this error:

它返回给我这个错误:

ImportError: No module named sklearn.cross_validation

What I need to do?

我需要做什么?

采纳答案by Dima Lituiev

It must relate to the renaming and deprecation of cross_validationsub-module to model_selection. Try substituting cross_validationto model_selection

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

回答by Richard Rublev

Make sure you have Anacondainstalled and then create a virtualenv using conda. This will ensure all the imports work

确保您已安装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

回答by nantitv

May be it's due to the deprecation of sklearn.cross_validation. Please replace sklearn.cross_validation with sklearn.model_selection

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

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

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

回答by ayat ullah sony

train_test_split is now in model_selection. Just type:

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

from sklearn.model_selection import train_test_split

it should work

它应该工作

回答by ROHAN YEOLE

Splitting the dataset into the Training set and Test set

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

from sklearn.model_selection import train_test_split

回答by Murat ?ahin

I guess cross selection is not active anymore. We should use instead model selection. You can write it to run, from sklearn.model_selection import train_test_split

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

Thats it.

就是这样。

回答by baya prakash reddy

Past : from sklearn.cross_validation(This package is deprecated in 0.18 version from 0.20 onwards it is changed to from sklearn import model_selection).

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

Present: from sklearn import model_selection

展示: from sklearn import model_selection

Example 2:

示例 2:

Past : from sklearn.cross_validation import cross_val_score(Version 0.18 which is deprecated)

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

Present : from sklearn.model_selection import cross_val_score

展示 : from sklearn.model_selection import cross_val_score

回答by Asad

sklearn.cross_validation

has changed to

已更改为

sklearn.model_selection

Checkout the documentation here: https://scikit-learn.org/stable/modules/cross_validation.html

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

回答by Tanmoy Bhowmick

sklearn.cross_validationis now changed to sklearn.model_selection

sklearn.cross_validation现在改为 sklearn.model_selection

Just use

只需使用

from sklearn.model_selection import train_test_split

I think that will work.

我认为这会奏效。

回答by Aniket Mukherjee

cross_validation was deprecated some time ago, try switching it out with model_selection

cross_validation 前段时间已被弃用,请尝试使用 model_selection 将其切换