StratifiedGroupKFoldRequiresGroups
A small wrapper around scikit-learn's StratifiedGroupKFold that makes the
groups argument mandatory when calling split().
What It Is
StratifiedGroupKFoldRequiresGroups is a subclass of
sklearn.model_selection.StratifiedGroupKFold. It keeps the underlying
cross-validation behavior from scikit-learn, but adds a guardrail: callers must
provide a non-None groups argument to split().
This is useful when grouped splitting is part of the correctness of a model
evaluation workflow. If a pipeline, estimator, or helper forgets to pass
groups, the split should fail immediately instead of silently behaving like a
regular stratified split without group isolation.
Installation
pip install StratifiedGroupKFoldRequiresGroups
The package requires Python 3.8 or newer and depends on scikit-learn.
Usage
import numpy as np
from StratifiedGroupKFoldRequiresGroups import StratifiedGroupKFoldRequiresGroups
X = np.random.randn(9, 5)
y = np.array(["class1", "class2", "class3"] * 3)
groups = np.array([
"group1",
"group2",
"group3",
"group4",
"group5",
"group6",
"group7",
"group7",
"group7",
])
cv = StratifiedGroupKFoldRequiresGroups(n_splits=3)
for train_index, test_index in cv.split(X, y, groups):
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]
The constructor is inherited from scikit-learn's StratifiedGroupKFold, so use
the same options such as n_splits, shuffle, and random_state.
Important Behavior
cv.split(X, y, groups)delegates tosklearn.model_selection.StratifiedGroupKFold.split().cv.split(X, y)raises aTypeErrorbecausegroupsis a required positional argument.cv.split(X, y, groups=None)raises aValueError.- The wrapper does not change scikit-learn's splitting algorithm; it only enforces that group labels are supplied.
Development
pip install -r requirements_dev.txt
pip install -e .
pytest
The test suite checks that the class is a StratifiedGroupKFold subclass, that
it produces the expected number of splits, and that missing or None groups
fail as intended.
Changelog
0.0.1
- First release on PyPI.
Release files for StratifiedGroupKFoldRequiresGroups 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| stratifiedgroupkfoldrequiresgroups-0.0.2.tar.gz | 5.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| stratifiedgroupkfoldrequiresgroups-0.0.2-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details |
Total release size:9.6 kB
Release files / stratifiedgroupkfoldrequiresgroups-0.0.2.tar.gz
| Download URL | stratifiedgroupkfoldrequiresgroups-0.0.2.tar.gz |
|---|---|
| Size | 5.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f6e213d5ca37649fa000aca36136d7f602cf9b26533667914129e3a392dde83c
|
|
BLAKE2b-256 checksum How to use checksums |
8d4cfa6fea8c664e0df26ebcb3f5a4ac54edc7b03cd9d4ffe1c70b32427f4662
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.25
|
Release files / stratifiedgroupkfoldrequiresgroups-0.0.2-py2.py3-none-any.whl
| Download URL | stratifiedgroupkfoldrequiresgroups-0.0.2-py2.py3-none-any.whl |
|---|---|
| Size | 4.4 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
bc8af13ab2948600db7860221c1eba6a598119567b5c7f24050f73c0b4d6b3b6
|
|
BLAKE2b-256 checksum How to use checksums |
cdbb15aadd07555b92e583cee43b69bdc1ea03dbe3d6932b901f8ec219a641b3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.25
|