Skip to content Skip to sidebar Skip to footer

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)

Solution 1:

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 documentation of LeaveOneOut for version 0.17 here where its mentioned that:

Parameters: n : int Total number of elements in dataset.

Answer :

  • Update the scikit-learn to version 0.18
  • Initialize the LeaveOneOut as follows:

    loo = LeaveOneOut(size of X_train_std)

Edit:

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]

Solution 2:

use

from sklearn.model_selectionimport train_test_split

rather than cross_validation because cross_validation in changed into model_selction

Post a Comment for "Issue With Cross Validation"