slope 6.0.1
Loading...
Searching...
No Matches
slope::Folds Class Reference

Manages data partitioning for cross-validation. More...

#include <folds.h>

Public Member Functions

 Folds ()
 Default constructor.
 
 Folds (int n_samples, int n_folds, int n_repeats=1, uint64_t seed=42)
 Constructor for generating random folds with optional repetitions.
 
 Folds (const std::vector< std::vector< int > > &user_folds)
 Constructor for user-provided folds.
 
 Folds (const std::vector< std::vector< std::vector< int > > > &user_folds)
 Constructor for user-provided repeated folds.
 
const std::vector< int > & getTestIndices (size_t fold_idx, size_t rep_idx=0) const
 Get test indices for a specific fold and repetition.
 
std::vector< int > getTrainingIndices (size_t fold_idx, size_t rep_idx=0) const
 Get training indices for a specific fold and repetition.
 
template<typename T >
auto split (Eigen::EigenBase< T > &x, const Eigen::MatrixXd &y, size_t fold_idx, size_t rep_idx=0) const
 Split data into training and test sets for a specific fold and repetition.
 
size_t numFolds () const
 Get the number of folds.
 
size_t numRepetitions () const
 Get the number of repetitions.
 
size_t numEvals () const
 Get the total number of folds (repetitions * folds)
 

Public Attributes

std::vector< std::vector< std::vector< int > > > folds
 Indices for each fold in each repetition.
 
std::size_t n_folds
 Number of folds.
 
std::size_t n_repeats
 Number of repetitions.
 

Detailed Description

Manages data partitioning for cross-validation.

This class handles the creation and access of training/test splits for k-fold cross-validation. It can generate random folds or use user-provided custom fold definitions.

Definition at line 25 of file folds.h.

Constructor & Destructor Documentation

◆ Folds() [1/4]

slope::Folds::Folds ( )
inline

Default constructor.

Creates an empty Folds object that can be assigned to later.

Definition at line 33 of file folds.h.

◆ Folds() [2/4]

slope::Folds::Folds ( int  n_samples,
int  n_folds,
int  n_repeats = 1,
uint64_t  seed = 42 
)
inline

Constructor for generating random folds with optional repetitions.

Parameters
n_samplesTotal number of samples in the dataset
n_foldsNumber of folds to create
n_repeatsNumber of repetitions (default: 1)
seedRandom seed for reproducibility

Creates k-fold partitions by randomly assigning samples to folds. When n_repeats > 1, multiple sets of folds are created with different random seeds.

Definition at line 51 of file folds.h.

◆ Folds() [3/4]

slope::Folds::Folds ( const std::vector< std::vector< int > > &  user_folds)
inlineexplicit

Constructor for user-provided folds.

Parameters
user_foldsVector of vectors containing indices for each fold

Uses the provided fold assignments instead of creating random folds.

Definition at line 70 of file folds.h.

◆ Folds() [4/4]

slope::Folds::Folds ( const std::vector< std::vector< std::vector< int > > > &  user_folds)
inlineexplicit

Constructor for user-provided repeated folds.

Parameters
user_foldsVector of fold sets for multiple repetitions

Definition at line 82 of file folds.h.

Member Function Documentation

◆ getTestIndices()

const std::vector< int > & slope::Folds::getTestIndices ( size_t  fold_idx,
size_t  rep_idx = 0 
) const

Get test indices for a specific fold and repetition.

Parameters
fold_idxIndex of the fold to use as test set
rep_idxIndex of the repetition (default: 0)
Returns
const std::vector<int>& Reference to the vector of test indices

◆ getTrainingIndices()

std::vector< int > slope::Folds::getTrainingIndices ( size_t  fold_idx,
size_t  rep_idx = 0 
) const

Get training indices for a specific fold and repetition.

Parameters
fold_idxIndex of the fold to exclude from training
rep_idxIndex of the repetition (default: 0)
Returns
std::vector<int> Vector containing all indices except those in the specified fold

◆ numEvals()

size_t slope::Folds::numEvals ( ) const
inline

Get the total number of folds (repetitions * folds)

Returns
size_t Number of evaluations

Definition at line 163 of file folds.h.

◆ numFolds()

size_t slope::Folds::numFolds ( ) const
inline

Get the number of folds.

Returns
size_t Number of folds

Definition at line 149 of file folds.h.

◆ numRepetitions()

size_t slope::Folds::numRepetitions ( ) const
inline

Get the number of repetitions.

Returns
size_t Number of repetitions

Definition at line 156 of file folds.h.

◆ split()

template<typename T >
auto slope::Folds::split ( Eigen::EigenBase< T > &  x,
const Eigen::MatrixXd &  y,
size_t  fold_idx,
size_t  rep_idx = 0 
) const
inline

Split data into training and test sets for a specific fold and repetition.

Template Parameters
TType of design matrix (supports both dense and sparse matrices)
Parameters
xInput feature matrix
yResponse matrix
fold_idxIndex of the fold to use as test set
rep_idxIndex of the repetition (default: 0)
Returns
std::tuple containing (x_train, y_train, x_test, y_test)

This method creates training and test datasets by subsetting the original data according to the specified fold indices. For dense matrices, it creates copies of the data. For sparse matrices, it creates efficiently constructed sparse matrix subsets.

Definition at line 127 of file folds.h.

Member Data Documentation

◆ folds

std::vector<std::vector<std::vector<int> > > slope::Folds::folds

Indices for each fold in each repetition.

Definition at line 166 of file folds.h.

◆ n_folds

std::size_t slope::Folds::n_folds

Number of folds.

Definition at line 167 of file folds.h.

◆ n_repeats

std::size_t slope::Folds::n_repeats

Number of repetitions.

Definition at line 168 of file folds.h.


The documentation for this class was generated from the following file: