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$

3 Answers 3

7
$\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$
5
  • 2
    $\begingroup$ it give me an error ValueError: n_splits=56 cannot be greater than the number of members in each class. whereas 56 is X.shape[0] $\endgroup$ Commented Jun 13, 2020 at 6:20
  • 1
    $\begingroup$ scores = cross_val_score(classifier , X = input data , y = target values , cv = Kfold(X.shape[0])) $\endgroup$ Commented Jun 13, 2020 at 6:28
  • $\begingroup$ Why does one get several scores? Is the algorithm calculating the performance score on the test set? and not on the left out validation observations? Otherwise, it should produce only one score, no? (I may create a separate question for this, later). $\endgroup$
    – Sapiens
    Commented Nov 13, 2020 at 22:15
  • $\begingroup$ @Sapiens You get a score each test, and you can then average the scores. $\endgroup$ Commented Nov 19, 2020 at 18:19
  • $\begingroup$ @PhillipCopley I see, you can indeed have several observations for the subject left out. $\endgroup$
    – Sapiens
    Commented Dec 18, 2020 at 17:21
2
$\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)

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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