pub struct StratifiedKFold<L> { /* private fields */ }Expand description
Stratified K-fold cross-validation.
Each fold keeps roughly the same class distribution as the full dataset. This ports the previously hand-rolled stratification logic from the guide’s Evaluation addendum into a real, tested implementation: samples are grouped by class label, then each class’s samples are distributed across the folds so every fold receives a proportional slice of every class.
The label type is generic (L: Eq + Hash + Clone), so string, integer or
enum labels all work with no forced mapping step — the same convention as the
sibling imbalance-rs crate. Labels are supplied at construction and stored,
which is what lets a stratified splitter satisfy the plain
CvSplitter interface (see the module docs).
§Small classes
A class with fewer than n_splits samples cannot appear in every fold. Such
a class is spread across as many folds as it can fill (one sample each,
leading folds first) and a warning is emitted on stderr — matching the
“warn and adjust rather than hard-error where reasonable” policy.
use ndarray::array;
use model_selection_rs::splitters::{CvSplitter, StratifiedKFold};
let y = array![0, 0, 0, 0, 1, 1, 1, 1];
let skf = StratifiedKFold::new(2, &y).unwrap();
let splits = skf.split(y.len()).unwrap();
// Each fold's test set holds two 0s and two 1s.
assert_eq!(splits.len(), 2);Implementations§
Source§impl<L: Eq + Hash + Clone> StratifiedKFold<L>
impl<L: Eq + Hash + Clone> StratifiedKFold<L>
Sourcepub fn new(n_splits: usize, y: &Array1<L>) -> Result<Self>
pub fn new(n_splits: usize, y: &Array1<L>) -> Result<Self>
Create a StratifiedKFold over the class labels y.
§Errors
Returns ModelSelectionError::InvalidSplitCount if n_splits < 2.
Sourcepub fn with_shuffle(self, seed: u64) -> Self
pub fn with_shuffle(self, seed: u64) -> Self
Shuffle each class’s samples (deterministically, from seed) before
distributing them across folds.
Trait Implementations§
Source§impl<L: Clone> Clone for StratifiedKFold<L>
impl<L: Clone> Clone for StratifiedKFold<L>
Source§fn clone(&self) -> StratifiedKFold<L>
fn clone(&self) -> StratifiedKFold<L>
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> CvSplitter for StratifiedKFold<L>
impl<L: Eq + Hash + Clone> CvSplitter for StratifiedKFold<L>
Auto Trait Implementations§
impl<L> Freeze for StratifiedKFold<L>
impl<L> RefUnwindSafe for StratifiedKFold<L>where
L: RefUnwindSafe,
impl<L> Send for StratifiedKFold<L>where
L: Send,
impl<L> Sync for StratifiedKFold<L>where
L: Sync,
impl<L> Unpin for StratifiedKFold<L>where
L: Unpin,
impl<L> UnsafeUnpin for StratifiedKFold<L>
impl<L> UnwindSafe for StratifiedKFold<L>where
L: 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