Issue with Cross Validation


I want to use leave one out cross validation. But i am getting below error:

AttributeError Traceback (most recent call last) <ipython-input-19-f15f1e522706> in <module>() 3 loo = LeaveOneOut(num_of_examples) 4 #loo.get_n_splits(X_train_std) ----> 5 for train, test in loo.split(X_train_std): 6 print("%s %s" % (train, test))

AttributeError: 'LeaveOneOut' object has no attribute 'split'

The detailed code is as follows:

from sklearn.cross_validation import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0) from sklearn.preprocessing import StandardScaler sc = StandardScaler() sc.fit(X_train) X_train_std = sc.transform(X_train) X_test_std = sc.transform(X_test) from sklearn.cross_validation import LeaveOneOut num_of_examples = len(X_train_std) loo = LeaveOneOut(num_of_examples) for train, test in loo.split(X_train_std): print("%s %s" % (train, test))


I think that you are using scikit-learn version below 0.18 and maybe referring some tutorials for version 0.18.

In versions prior to 0.18, the LeaveOneOut() constructor has a required parameter n which is not supplied in the above code you posted. Hence the error. You can refer to the <a href="http://scikit-learn.org/0.17/modules/generated/sklearn.cross_validation.LeaveOneOut.html#sklearn.cross_validation.LeaveOneOut" rel="nofollow">documentation of LeaveOneOut for version 0.17 here</a> where its mentioned that:


Parameters: <strong>n</strong> : int Total number of elements in dataset.



<ul><li>Update the scikit-learn to version 0.18</li> <li>

Initialize the <strong>LeaveOneOut</strong> as follows:

loo = LeaveOneOut(size of X_train_std)

</li> </ul>


If you are using the scikit version >=0.18:

from sklearn.model_selection import LeaveOneOut for train_index, test_index in loo.split(X): print("%s %s" % (train_index, test_index)) X_train, X_test = X[train_index], X[test_index] y_train, y_test = y[train_index], y[test_index]

Else, for versions < 0.18 use the iterations like this (Notice that here loo.split() is not used, loo is used directly):

from sklearn.cross_validation import LeaveOneOut loo = LeaveOneOut(num_of_examples) for train_index, test_index in loo: print("%s %s" % (train_index, test_index)) X_train, X_test = X[train_index], X[test_index] y_train, y_test = y[train_index], y[test_index]


  • Unexpected behavior from xgboost in Python with Custom Evaluation Function
  • AttributeError: 'numpy.int64' object has no attribute 'timestamp' in python 3.5
  • How to predict the results for OCR using keras image_ocr example?
  • How to get a normalised slope of a trend
  • ValueError: x and y must be the same size
  • how to force scikit-learn DictVectorizer not to discard features?
  • negative value for “mean_squared_error”
  • Python equivalent to R caTools random 'sample.split'
  • Exporting python sklearn models to production (java/c++)
  • how does sklearn's Adaboost predict_proba works internally?
  • Python: *args unpacks, how to repack from test_train_split?
  • Any difference between H2O and Scikit-Learn metrics scoring?
  • Scikit-learn Imputer Reducing Dimensions
  • How to get feature Importance in naive bayes?
  • GridSearch for Multi-label classification in Scikit-learn
  • Update data in d3.js group
  • jquery draggable stop event
  • How to get a android ListView item selector to use state_pressed
  • Inserting a (g) node in the middle of a tree (SVG) using jQuery
  • Build an array of ids of all select boxes
  • changes in jquery 1.4.2 breaking the code?
  • jquery code not working without breakpoint
  • SAVE attribute needed for Fortran variables when only the C_LOC address is returned to a C program?
  • saving file generated by TCPDF
  • d3 v4 drag and drop with TypeScript
  • Checking free space on FTP server
  • Can I check if a recipient has an automatic reply before I send an email?
  • Matplotlib draw Spline from multiple points
  • Why winpcap requires both .lib and .dll to run?
  • align graphs with different xlab
  • Akka Routing: Reply's send to router ends up as dead letters
  • Return words with double consecutive letters
  • AT Commands to Send SMS not working in Windows 8.1
  • Python: how to group similar lists together in a list of lists?
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • How do I configure my settings file to work with unit tests?
  • Is it possible to post an object from jquery to bottle.py?
  • Busy indicator not showing up in wpf window [duplicate]
  • Reading document lines to the user (python)
  • Python/Django TangoWithDjango Models and Databases