KingCambo
KingCambo

Reputation: 13

How to solve a TypeError using LeaveOneOut

I have been trying to work through the Vanderplass book and I have been stuck on this cell for days now:

from sklearn.model_selection import cross_val_score
cross_val_score(model, X, y, cv=5)

from sklearn.model_selection import LeaveOneOut
scores = cross_val_score(model, X, y, cv=LeaveOneOut(len(X)))
scores

TypeError                                 Traceback (most recent call last)
<.  ipython-input-78-029fa0c72898> in <module>
  1 from sklearn.model_selection import LeaveOneOut
 ----> 2 scores = cross_val_score(model, X, y, cv=LeaveOneOut(len(X)))
  3 scores

TypeError: LeaveOneOut() takes no arguments

import sklearn
sklearn.__version__
0.22.1'

Thanks in advance for any help! Cameron

Upvotes: 1

Views: 1835

Answers (2)

Kaushal Prasad
Kaushal Prasad

Reputation: 1

or, can also use

from sklearn.model_selection import cross_val_score
cross_val_score(model, X, y, cv=5)

from sklearn.model_selection import LeaveOneOut
scores = cross_val_score(model, X, y, cv=LeaveOneOut().split(X))
scores

Upvotes: 0

Max Power
Max Power

Reputation: 8996

Welcome to Stack Overflow!

The error says LeaveOneOut() takes no arguments, but when you instantiated LeaveOneOut you passed it len(X) as an argument (in LeaveOneOut(len(X))).

If you change your scores line to the line below it should work:

scores = cross_val_score(model, X, y, cv=LeaveOneOut())

However, note this warning from the scikit-learn documentation:

Note: LeaveOneOut() is equivalent to KFold(n_splits=n)...Due to the high number of test sets (which is the same as the number of samples) this cross-validation method can be very costly. For large datasets one should favor KFold, ShuffleSplit or StratifiedKFold.

In case that's not clear, that's a suggestion to use e.g. KFold with like n=5, which is usually going to serve you better than LeaveOneOut.

Upvotes: 1

Related Questions