cleverly.learners.random_partition#
- cleverly.learners.random_partition(n: int, n_folds: int, *, cluster: ndarray[tuple[Any, ...], dtype[int64]] | None = None, seed: int) Folds[source]#
Draw an unstratified split of rows, or of whole clusters, from a seed.
The draw reads no outcome, treatment or covariate. The assignment depends on
n, the cluster labels andseedalone, so the same inputs give the same split on every supported scikit-learn.- Parameters:
- Returns:
- Raises:
ValueErrorIf
nis below two, the seed is not an integer in range, or fewer than two folds are possible.
See also
make_foldsBuilds a split with or without strata, and calls this function for an unstratified split.
FoldOriginThe record this function attaches to the split.
Notes
The row-level draw shuffles
numpy.arange(n)withnumpy.random.RandomState(seed). It then cuts the order intoKcontiguous blocks ofn // Krows, and the firstn % Kblocks take one extra row. This is the splitsklearn.model_selection.KFold(K, shuffle=True, random_state=seed)makes.The grouped draw permutes the sorted distinct cluster labels with
numpy.random.RandomState(seed)and cuts the permutation intoKparts withnumpy.array_split. This is the splitsklearn.model_selection.GroupKFold(K, shuffle=True, random_state=seed)makes in scikit-learn 1.6 and later. The number of clusters in two folds differs by at most one, and a cluster’s size does not change where it lands.Examples
>>> from cleverly.learners import random_partition >>> folds = random_partition(6, 3, seed=0) >>> folds.assignment.tolist() [2, 1, 0, 1, 2, 0] >>> folds.origin.scheme, folds.origin.seed ('vfold', 0)