Python 导入错误:没有名为 model_selection 的模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40704484/
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
ImportError: No module named model_selection
提问by Dims
I am trying to use train_test_split
function and write:
我正在尝试使用train_test_split
函数并编写:
from sklearn.model_selection import train_test_split
and this causes
这导致
ImportError: No module named model_selection
Why? And how to overcome?
为什么?以及如何克服?
回答by linusg
I guess you have the wrong version of scikit-learn
, a similar situation was described here on GitHub. Previously (before v0.18
), train_test_split
was located in the cross_validation
module:
我猜你有错误的版本scikit-learn
,在 GitHub 上描述了类似的情况。以前(之前v0.18
)train_test_split
位于cross_validation
模块中:
from sklearn.cross_validation import train_test_split
However, now it's in the model_selection
module:
但是,现在它在model_selection
模块中:
from sklearn.model_selection import train_test_split
so you'll need the newest version.
所以你需要最新的版本。
To upgrade to at least version 0.18
, do:
要升级到至少 version 0.18
,请执行以下操作:
pip install -U scikit-learn
(Or pip3
, depending on your version of Python). If you've installed it in a different way, make sure you use another method to update, for example when using Anaconda.
(或者pip3
,取决于您的 Python 版本)。如果您以不同的方式安装它,请确保使用其他方法进行更新,例如在使用 Anaconda 时。
回答by Farhad Maleki
Update sklearn
更新sklearn
conda update scikit-learn
conda 更新 scikit-learn
回答by curry_xyd
I encountered this problem when I import GridSearchCV
.
我在导入时遇到了这个问题GridSearchCV
。
Just changed sklearn.model_selection
to sklearn.grid_search
.
刚换sklearn.model_selection
到sklearn.grid_search
。
回答by Uki D. Lucas
I had the same problem while using Jupyter Notebook, no matter what I updated in Python 3, conda, I could not get in Jupyter:
我在使用 Jupyter Notebook 时遇到了同样的问题,无论我在 Python 3、conda 中更新了什么,我都无法进入 Jupyter:
import sklearn
print (sklearn.__version__)
0.17.1
to SHOW scikit-learn-0.18.1
显示 scikit-learn-0.18.1
Finally, I removed Anaconda3 and Jupyter Notebook and reinstalled fresh. I got it to work.
最后,我删除了 Anaconda3 和 Jupyter Notebook 并重新安装。我让它工作。
http://ukitech.blogspot.com/2017/02/sklearnmodelselection.html
http://ukitech.blogspot.com/2017/02/sklearnmodelselection.html
回答by Alex L
do you have sklearn? if not, do the following:
你有sklearn吗?如果没有,请执行以下操作:
sudo pip install sklearn
After installing sklearn
安装sklearn后
from sklearn.model_selection import train_test_split
works fine
工作正常
回答by Minh Vo
To install scikit-learn version 18.0, I used both commands:
要安装 scikit-learn 18.0 版,我使用了两个命令:
conda update scikit-learn
conda 更新 scikit-learn
pip install -U scikit-learn
pip install -U scikit-learn
But it does not work. There was a problem "Cannot install 'scikit-learn'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall".
但它不起作用。有一个问题“无法安装‘scikit-learn’。这是一个distutils安装的项目,因此我们无法准确确定哪些文件属于它,只会导致部分卸载”。
Finally, i can install it by using following command:
最后,我可以使用以下命令安装它:
pip install --user --upgrade scikit-learn==0.18.0
pip install --user --upgrade scikit-learn==0.18.0
回答by Paras Patidar
In Late September 2016, SciKit Learn 0.18 was released and there was a slight change to the code. With SciKit Learn 0.18 the train_test_split function is now imported from model_selection instead of cross_validation.
2016 年 9 月下旬,SciKit Learn 0.18 发布,代码略有变化。在 SciKit Learn 0.18 中,train_test_split 函数现在从 model_selection 而不是 cross_validation 导入。
from sklearn.cross_validation import train_test_split
has been changed to :
已更改为:
from sklearn.model_selection import train_test_split
The same has also happened for GridSearchCV.
GridSearchCV 也发生了同样的情况。
回答by Sameer Kumar Choudhary
Latest Stable release of sklearn 0.20.0 has train_test_splitis under model_selection not under cross_validation
sklearn 0.20.0 的最新稳定版本已将train_test_split置于 model_selection 下而不是cross_validation
In order to check your sklearn version :
为了检查您的 sklearn 版本:
import sklearn print (sklearn.version) 0.20.2
进口sklearn打印(sklearn。版)0.20.2
回答by MCardus
As @linusg said, one option is just import crossvalidation as follows:
正如@linusg 所说,一种选择就是导入交叉验证,如下所示:
from sklearn import cross_validation
X_train,X_test,y_train,y_test = cross_validation.train_test_split(X,y,test_size=0.3)
回答by MMF
Adding some infoto the previous answer from @linusg :
在@linusg 的上一个答案中添加一些信息:
sklearn
keeps a release history of all its changes. Think of checking it from time to time. Hereis the link to the documentation.
sklearn
保留所有更改的发布历史记录。想不时检查一下。这是文档的链接。
As you can see in the documentation for the version 0.18, a new module was created called model_selection
. Therefore it didn't exist in previous versions.
正如您在0.18版的文档中所见,创建了一个名为model_selection
. 因此它在以前的版本中不存在。
Update sklearn
and it will work !
更新sklearn
,它将起作用!