Skip to content

sklearn/random-state-without-shuffle

Pack Severity Fix Versions
scikit-learn error unsafe, with xpd check --fix --unsafe-fixes All versions

KFold uses random_state only when it shuffles, so without shuffle=True the call raises ValueError. Pass shuffle=True, or drop random_state.

folds = KFold(n_splits=5, random_state=42)
folds = KFold(n_splits=5, shuffle=True, random_state=42)

The rule reports KFold(n_splits=5, random_state=42) without shuffle=True, which raises ValueError.

xpd check --fix --unsafe-fixes adds shuffle=True when random_state is passed and shuffle is omitted. Calls that explicitly pass shuffle=False remain manual so you can decide whether to enable shuffling or drop random_state.

The rule covers KFold, StratifiedKFold and StratifiedGroupKFold, and GroupKFold from scikit-learn 1.6, which added its shuffle. Each raises "Setting a random_state has no effect since shuffle is False" on scikit-learn 1.9.1. A file that imports the removed sklearn.cross_validation is left alone unless the call passes n_splits, because the old classes took other arguments. removed-in-sklearn reports the import in such a file.

The rule as the readers saw it gave wrong advice in 2 and 2 of 11 labelled reports, on such files, and the narrowed rule no longer makes the reports. Recall was 54 of 54.

$K uses random_state only when it shuffles, so without shuffle=True it raises a ValueError. Pass shuffle=True, or drop random_state.

See all rules in the scikit-learn pack.