Python 模块“sklearn”没有属性“cross_validation”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46572475/
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 17:42:28  来源:igfitidea点击:

module 'sklearn' has no attribute 'cross_validation'

pythonscikit-learncross-validation

提问by Naren

I am trying to split my dataset into training and testing dataset, but I am getting this error:

我试图将我的数据集拆分为训练和测试数据集,但出现此错误:

X_train,X_test,Y_train,Y_test = sklearn.cross_validation.train_test_split(X,df1['ENTRIESn_hourly'])


AttributeError                            Traceback (most recent call last)
<ipython-input-53-5445dab94861> in <module>()
----> 1 X_train,X_test,Y_train,Y_test = sklearn.cross_validation.train_test_split(X,df1['ENTRIESn_hourly'])

AttributeError: module 'sklearn' has no attribute 'cross_validation'

How can I handle this?

我该如何处理?

回答by Brenden Petersen

sklearndoes not automatically import its subpackages. If you only imported via: import sklearn, then it wont work. Import with import sklearn.cross_validationinstead.

sklearn不会自动导入其子包。如果您仅通过以下方式导入:import sklearn,则它将无法正常工作。用import sklearn.cross_validation代替导入。

Furhter, sklearn.cross_validationwill be deprecated in version 0.20. Use sklearn.model_selection.train_test_splitinstead.

此外,sklearn.cross_validation将在 0.20 版中弃用。使用sklearn.model_selection.train_test_split来代替。

回答by gogasca

Try this:

尝试这个:

from sklearn.model_selection import train_test_split
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.33, random_state=101)

回答by Enrique Benito Casado

The equivalent to cross_validation in sklearn is:

sklearn 中 cross_validation 的等价物是:

  sklearn.model_selection

回答by Joel K Thomas

"cross_validation" name is now deprecated and was replaced by "model_selection" inside the new anaconda versions. So you can use

“cross_validation”名称现已弃用,并在新 anaconda 版本中由“model_selection”替代。所以你可以使用

from sklearn.model_selection import train_test_split

回答by Rajendra Prasad Taidala

you can try this

你可以试试这个

X_train,X_test,Y_train,Y_test = 
    sklearn.model_selection.train_test_split(X,boston_df.price)