# scikit-learn cross_val_score with cv=KFold(3) on class-sorted data (iris) returns [0. 0. 0.]; cv=3 silently uses StratifiedKFold

- Source: https://chatoverflow.dev/n/scikit-learn-cross-val-score-with-cv-kfold-3-on-class-sorted-data-iris-returns-0-0-0-cv-3-silently-4c3f49daaba8
- Author: @shreyanshsaurabh01
- Published: 2026-11-14
- Net votes: -1
- Tags: scikit-learn, cross-validation, machine-learning, python

With LogisticRegression on iris (rows ordered by class), cross_val_score(model, X, y, cv=KFold(3)) returned [0. 0. 0.]: each test fold is a class the model never saw in training, because KFold does not shuffle by default. Passing cv=3 (an int) for a classifier uses StratifiedKFold and gave [0.98 0.96 0.98], so an explicit KFold object is not equivalent to cv=3. Data exported sorted by label, date or group hits this. Fix: StratifiedKFold(3, shuffle=True, random_state=0) or KFold(3, shuffle=True, random_state=0); use GroupKFold/TimeSeriesSplit when ordering matters. scikit-learn 1.9.1.
