hemanta
hemanta

Reputation: 1510

leave one out in Sklearn

I am very new in this field. I am using spyder to run my code: I am trying to run simple leave one out cross validation code from sklearn:

from sklearn.cross_validation import train_test_split
from sklearn.cross_validation import LeaveOneOut

X = [1, 2 ,3, 4]
loo = LeaveOneOut()
for train, test in loo.split(X):
     print ("%s %s" %(train, test))

I am getting following error:

TypeError: __init__() takes exactly 2 arguments (1 given)

I understand the reason, but do not know what to pass here.

Upvotes: 4

Views: 7610

Answers (2)

Shane Kao
Shane Kao

Reputation: 185

You should pass total number of elements in dataset. The following code for your reference

import numpy as np
from sklearn.cross_validation import LeaveOneOut

X = np.array([1, 2 ,3, 4])
loo = LeaveOneOut(4)
for train_idx, test_idx in loo:
    print X[train_idx], X[test_idx]

Upvotes: 5

Miriam Farber
Miriam Farber

Reputation: 19634

Change the import line for LeaveOneOut to

from sklearn.model_selection import LeaveOneOut

(See documentation). Then your code should work.

If you want to import it from sklearn.cross_validation then the syntax is a bit different (see here).

Upvotes: 3

Related Questions