eman
eman

Reputation: 195

How to get the folds themselves that are partitioned internally in sklearn.cross_validation.cross_val_score?

I'm using:

sklearn.cross_validation.cross_val_score

to make a cross validation and get the results of each run.

The output of this function is the scores.

Is there a method to get the folds (partitions) themselves that are partitioned internally in the cross_val_score function?

Upvotes: 1

Views: 1608

Answers (2)

YS-L
YS-L

Reputation: 14738

There isn't a way to extract the internal cross validation splits used in the cross_val_score, as this function does not expose any state about it. As mentioned in the documentation, either a k-fold or stratified k-fold with k=3 will be used.

However, if you need to keep track of the cross validation splits used, you can explicitly pass in the cv argument of cross_val_score by creating your own cross validation iterators:

from sklearn.cross_validation import KFold, cross_val_score
from sklearn.datasets import load_iris
from sklearn.svm import SVC

iris = load_iris()
kf = KFold(len(iris.target), 5, random_state=0)
clf = SVC(kernel='linear', C=1)
scores = cross_val_score(clf, iris.data, iris.target, cv=kf)

so that it uses the splits you specified exactly instead of rolling its own.

Upvotes: 2

Kyle Kastner
Kyle Kastner

Reputation: 1018

The default cross validator for cross_val_score is a StratifiedKFold with K=3 for classification. You can get a cross validation iterator instead, by using the StratifiedKFold and looping over the splits as shown in the example.

Upvotes: 2

Related Questions