Scikit learn(Python 3.5):我是否需要导入库才能使其工作?

时间:2017-04-08 07:50:40

标签: 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(最近一次调用   最后)in()         1 selected_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打印(“(X列车形状%s,X测试形状%s,\ n列车形状%s,y测试形状%s”%(X_train.shape,X_test.shape,y_train.shape,   y_test.shape))         4 h1.fit(X_train,y_train)         5打印(h1.score(X_test,y_test))

     

NameError:未定义名称'cross_validation'

我怀疑我可能要导入一本书未提及的图书馆。我搜索了手册但找不到这个功能。这是我需要创建的功能,还是有人可以指向相关的库?

5 个答案:

答案 0 :(得分:6)

MouseManager& GameManager::getMouseManager(){ return mouseManager; } 的{​​p} cross_validation子模块为deprecated。您应该使用sklearn instead

module_selection

答案 1 :(得分:0)

您必须导入:

from sklearn.svm.libsvm import cross_validation

答案 2 :(得分:0)

您应该导入

from sklearn import cross_validation

确保你已经安装了sklearn。有关如何安装

的说明,请参阅this

答案 3 :(得分:0)

这应该可以解决您的问题。试试

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = 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)

答案 4 :(得分:0)

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)   
 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)