Suppose I want to do k-fold cross-validation across a set of data points. Each of these data points is a member of exactly one of n classes. (For example, suppose each of the data points is a person and the class they belong to is their age in years.) I want the distribution across classes of each of my k test sets to (as closely as possible) 'match' the distribution of classes across all the data points. (And I want each data point to be in exactly one of the k test sets.) The number of points in each of the n classes need not be a multiple of k.

Is there some well-known algorithm, or, better yet, Python library for doing this?


1 Answer 1


I believe you are looking for Stratified K-Fold Cross Validation, which preserves the distribution of classes across folds of the data.

Scikit-learn has a StratifiedKFold function for Python users!


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.