Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scikit learn (Python 3.5): Do I need to import a library to make this work?

I am working through Python Data Science Essentials (2nd Edition).

The book provides the code below:

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

When I try to run it, I get the following error:

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

I suspect that I might have to import a library which the book is not mentioning. I have searched the manuals but cannot find this function. IS this a function I need to create or can someone point me to the relevant library?

like image 299
Samuel Dare Avatar asked Apr 08 '17 07:04

Samuel Dare


Video Answer


1 Answers

cross_validation submodule of sklearn is deprecated. You should use 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)
...
like image 82
kvorobiev Avatar answered Sep 22 '22 19:09

kvorobiev