1
$\begingroup$

I have 52 CSV files in a folder. I want to build a model based on this data. That's why I want to Leave one out cross-validation on these data. How can I do this using sci-kit learn in python?

I tried from sci kit document and also search many resources.

import glob
import numpy as np
import pandas as pd
from sklearn.cross_validation import LeaveOneOut
path=r'...................\Data\New 
design process data'
filelist=glob.glob(path + "/*.csv")
loo=LeaveOneOut()
for train,test in loo.split(filelist):
   print("%s %s" % (train, test))

But it showed errors.

init() missing 1 required positional argument: 'n'

I am new in python as well as sci-kit learn. If anyone can help me, It would be a great convenience.

$\endgroup$
5
$\begingroup$

Leave One Out Cross Validation is just a special case of K- Fold Cross Validation where the number of folds = the number of samples in the dataset you want to run cross validation on.

For Python , you can do as follows:

from sklearn.model_selection import cross_val_score
scores = cross_val_score(classifier , X = input data , y = target values , cv = X.shape[0]) 

Here , cv = the number of folds . As cv = number of samples here , we will get Leave One Out Cross Validation. The length of list(scores) will be equal to number of samples in input data.

$\endgroup$
0
$\begingroup$

I think it is because you are using older version of scikit learn. Try to use model_selection module in newer version. I am using 0.19.1 and I got this-

>>> import sklearn
>>> sklearn.__version__
'0.19.1'
>>> from sklearn.model_selection import LeaveOneOut
>>> loo = LeaveOneOut()

As you can see, no errors are here. Now try using cross_validation-

>>> from sklearn.cross_validation import LeaveOneOut as LOO
C:\Users\USER\AppData\Local\Continuum\Anaconda3\lib\site-packages\sklearn\cross_validation.py:41: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.
  "This module will be removed in 0.20.", DeprecationWarning)
>>> loo = LOO()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __init__() missing 1 required positional argument: 'n'
$\endgroup$
0
$\begingroup$

The answer provided by Faraz is a nice solution to the problem of performing leave-one-out cross validation with sklearn, and nicely sidesteps the problem of the original poster.
But to come back to the original question, how to resolve the error? Apparently there are two versions of LeaveOneOut in sklearn:

from sklearn.cross_validation import LeaveOneOut  #(of the original poster)

and

from sklearn.model_selection import LeaveOneOut   #(which you can find easily online)

wich have a slightly different interface (cross_validation and model_selection). The latter can be run without parameters (as the original poster did), the former (which the original poster used) requires at least one parameter (n: Total number of elements):

  import glob
  import numpy as np
  import pandas as pd
  from sklearn.cross_validation import LeaveOneOut
  path=r'...................\Data\New 
  design process data'
  filelist=glob.glob(path + "/*.csv")
  loo=LeaveOneOut(n=52)
  for train,test in loo.split(filelist):
     print("%s %s" % (train, test))
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.