我在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
我需要做什么?
它必须与cross_validation
子模块到model_selection
的重命名和弃用有关。尝试将cross_validation
替换为model_selection
train_testrongplit现在处于model_selection中。仅类型:
from sklearn.model_selection import train_test_split
它应该工作
我想交叉选择不再处于活动状态。我们应该使用模型选择。你可以把它写下来运行,from sklearn.model_selection import train_test_split
就是这样。
确保安装了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
sklearn.cross_validation
现在更改为sklearn.model_selection
只需使用
from sklearn.model_selection import train_test_split
我认为这会奏效。
sklearn.cross_validation
已更改为
sklearn.model_selection
请在此处查看文档:https://scikit-learn.org/stable/modules/cross_validation.html
可能是由于sklearn.cross_validation的弃用。请将sklearn.cross_validation替换为sklearn.model_selection
参考-https://github.com/amueller/scipy_2015_sklearn_tutorial/issues/60
将数据集拆分为训练集和测试集
from sklearn.model_selection import train_test_split
过去: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,已弃用(
当前:from sklearn.model_selection import cross_val_score
sklearn.cross_validation
现在更改为sklearn.model_selection
只需更改
sklearn.cross_validation
至
sklearn.model_selection
cross_validation
不再可用。
尝试使用model_selection
而不是cross_validation
:
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
像这样更改代码
# from sklearn.cross_validation import train_test_split
from sklearn.model_selection import train_test_split
train_test_split
是模块sklearn.model_selection
的一部分,因此,您可能需要从model_selection
导入模块
代码:
from sklearn.model_selection import train_test_split
cross_validation在一段时间前被弃用,请尝试使用model_selection 将其切换出来