pub struct StratifiedGroupKFold<L, G> { /* private fields */ }Expand description
K-fold that tries to satisfy two constraints at once: keep class proportions balanced across folds and never let a group straddle the train/test boundary.
§Approximation
Perfectly satisfying both constraints simultaneously is not always possible —
a group is indivisible, so its whole class makeup lands in one fold. This
implementation therefore uses the same documented greedy heuristic as
scikit-learn’s StratifiedGroupKFold, and does not claim exactness:
- Count each class within each group.
- Visit groups in order of decreasing spread (standard deviation) of their per-class counts, so the “lumpiest” groups are placed first while there is still freedom to balance around them.
- Place each group in whichever fold keeps the per-fold class distribution closest to uniform (minimising the mean, over classes, of the standard deviation of each class’s per-fold share); ties break toward the smaller fold.
Group integrity is exact (groups are never split); only class balance is
approximate. n_splits must not exceed the number of distinct groups.
use ndarray::array;
use model_selection_rs::splitters::{CvSplitter, StratifiedGroupKFold};
let y = array![0, 0, 1, 1, 0, 1, 0, 1];
let groups = array![1, 1, 2, 2, 3, 3, 4, 4];
let sgkf = StratifiedGroupKFold::new(2, &y, &groups).unwrap();
let splits = sgkf.split(y.len()).unwrap();
assert_eq!(splits.len(), 2);Implementations§
Source§impl<L: Eq + Hash + Clone, G: Eq + Hash + Clone> StratifiedGroupKFold<L, G>
impl<L: Eq + Hash + Clone, G: Eq + Hash + Clone> StratifiedGroupKFold<L, G>
Sourcepub fn new(n_splits: usize, y: &Array1<L>, groups: &Array1<G>) -> Result<Self>
pub fn new(n_splits: usize, y: &Array1<L>, groups: &Array1<G>) -> Result<Self>
Create a StratifiedGroupKFold over class labels y and groups.
§Errors
Returns ModelSelectionError::InvalidSplitCount if n_splits < 2, or
ModelSelectionError::ShapeMismatch if y and groups differ in
length.
Trait Implementations§
Source§impl<L: Clone, G: Clone> Clone for StratifiedGroupKFold<L, G>
impl<L: Clone, G: Clone> Clone for StratifiedGroupKFold<L, G>
Source§fn clone(&self) -> StratifiedGroupKFold<L, G>
fn clone(&self) -> StratifiedGroupKFold<L, G>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<L: Eq + Hash + Clone, G: Eq + Hash + Clone> CvSplitter for StratifiedGroupKFold<L, G>
impl<L: Eq + Hash + Clone, G: Eq + Hash + Clone> CvSplitter for StratifiedGroupKFold<L, G>
Auto Trait Implementations§
impl<L, G> Freeze for StratifiedGroupKFold<L, G>
impl<L, G> RefUnwindSafe for StratifiedGroupKFold<L, G>where
L: RefUnwindSafe,
G: RefUnwindSafe,
impl<L, G> Send for StratifiedGroupKFold<L, G>
impl<L, G> Sync for StratifiedGroupKFold<L, G>
impl<L, G> Unpin for StratifiedGroupKFold<L, G>
impl<L, G> UnsafeUnpin for StratifiedGroupKFold<L, G>
impl<L, G> UnwindSafe for StratifiedGroupKFold<L, G>where
L: UnwindSafe,
G: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more