python - Scikit 学习(Python 3.5): Do I need to import a library to make this work?

标签 python python-3.x scikit-learn

我正在处理 Python Data Science Essentials (2nd Edition) .

书中提供了如下代码:

chosen_random_state = 1
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.30, ran-dom_state=chosen_random_state)
print ("(X train shape %s, X test shape %s, \ny train shape %s, y test shape %s" \
% (X_train.shape, X_test.shape, y_train.shape, y_test.shape))
h1.fit(X_train,y_train)
print (h1.score(X_test,y_test)) 

当我尝试运行它时,出现以下错误:

--------------------------------------------------------------------------- 
NameError                                 Traceback (most recent call last) <ipython-input-137-c5136df13468> in <module>()
          1 chosen_random_state = 1
    ----> 2 X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.30, random_state=chosen_random_state)
          3 print ("(X train shape %s, X test shape %s, \ny train shape %s, y test shape %s" % (X_train.shape, X_test.shape, y_train.shape, y_test.shape))
          4 h1.fit(X_train,y_train)
          5 print (h1.score(X_test,y_test))
    
    NameError: name 'cross_validation' is not defined

我怀疑我可能必须导入书中未提及的库。我已经搜索了手册,但找不到此功能。这是我需要创建的函数吗?有人可以指点我相关的库吗?

最佳答案

sklearn

cross_validation 子模块是 deprecated .你应该使用 sklearn.model_selection instead

from sklearn import model_selection

...
X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y, test_size=0.30, random_state=chosen_random_state)
...

关于python - Scikit 学习(Python 3.5): Do I need to import a library to make this work?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43291548/

相关文章:

python - 根据数据类型从 DataFrame 中删除列

python-3.x - Python (c) 配置文件 : Error importing SortKey from pstats

python - 超过 3 个类的 Scikit-learn (sklearn) 混淆矩阵图

python - 如何根据 python(pandas)中的用户输入删除列?

python - 使用 open cv 检测弧的中心

python - 基本 HTTPServer 卡在 "socket.readinto : return self._sock.recv_into(b)"

python - 对具有 x 和 y 误差的数据拟合幂律函数

python - 如何保存和恢复我的sklearn模型?

python - 在 python 中交换 numpy 数组中的两行

python - 在centos 6上安装Opencv 3.0.0