Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RSampler.hxx
Go to the documentation of this file.
1// Author: Martin Føll, University of Oslo (UiO) & CERN 01/2026
2
3/*************************************************************************
4 * Copyright (C) 1995-2026, Rene Brun and Fons Rademakers. *
5 * All rights reserved. *
6 * *
7 * For the licensing terms see $ROOTSYS/LICENSE. *
8 * For the list of contributors see $ROOTSYS/README/CREDITS. *
9 *************************************************************************/
10
11#ifndef ROOT_INTERNAL_ML_RSAMPLER
12#define ROOT_INTERNAL_ML_RSAMPLER
13
14#include <memory>
15#include <string>
16#include <vector>
17
19
20// Forward decls
22class RFlat2DMatrixOperators;
23}
24
26/**
27\class ROOT::Experimental::Internal::ML::RSampler
28
29\brief Implementation of different sampling strategies.
30*/
31
32class RSampler {
33private:
35 // rows of fDataset in each class
36 std::vector<std::vector<std::size_t>> &fRows;
37 std::string fSampleType;
41 std::size_t fSetSeed;
42 std::size_t fNumEntries;
43
44 std::size_t fMajor;
45 std::size_t fMinor;
46 std::size_t fNumMajor;
47 std::size_t fNumMinor;
48 std::size_t fNumResampledMajor;
49 std::size_t fNumResampledMinor;
50
51 std::vector<std::size_t> fSamples;
52
53 std::unique_ptr<RFlat2DMatrixOperators> fTensorOperators;
54
55public:
56 RSampler(const RFlat2DMatrix &dataset, std::vector<std::vector<std::size_t>> &rows, const std::string &sampleType,
57 float sampleRatio, bool replacement = false, bool shuffle = true, std::size_t setSeed = 0);
58
60
61 void SetupSampler();
62
63 void Sampler(RFlat2DMatrix &SampledTensor);
64
66
68
69 void RandomUndersampler(RFlat2DMatrix &ShuffledTensor);
70
71 void RandomOversampler(RFlat2DMatrix &ShuffledTensor);
72
73 void GatherSamples(RFlat2DMatrix &ShuffledTensor, const std::vector<std::size_t> &ResampledRows,
74 std::size_t numResampled, const std::vector<std::size_t> &OtherRows);
75
76 void SampleWithReplacement(std::size_t n_samples, std::size_t max);
77
78 void SampleWithoutReplacement(std::size_t n_samples, std::size_t max);
79
80 std::size_t GetNumEntries() { return fNumEntries; }
81};
82
83} // namespace ROOT::Experimental::Internal::ML
84#endif // ROOT_INTERNAL_ML_RSAMPLER
Implementation of different sampling strategies.
Definition RSampler.hxx:32
void SampleWithoutReplacement(std::size_t n_samples, std::size_t max)
Add indices without replacement to fSamples.
Definition RSampler.cxx:184
void SetupRandomUndersampler()
Calculate fNumEntries and major/minor variables for the random undersampler.
Definition RSampler.cxx:57
void GatherSamples(RFlat2DMatrix &ShuffledTensor, const std::vector< std::size_t > &ResampledRows, std::size_t numResampled, const std::vector< std::size_t > &OtherRows)
Fill the sampled entries: the resampled entries followed by all the other ones, in shuffled order.
Definition RSampler.cxx:131
void RandomOversampler(RFlat2DMatrix &ShuffledTensor)
Oversample entries randomly from the minority dataset.
Definition RSampler.cxx:118
std::vector< std::vector< std::size_t > > & fRows
Definition RSampler.hxx:36
void SampleWithReplacement(std::size_t n_samples, std::size_t max)
Add indices with replacement to fSamples.
Definition RSampler.cxx:151
void SetupRandomOversampler()
Calculate fNumEntries and major/minor variables for the random oversampler.
Definition RSampler.cxx:83
void SetupSampler()
Calculate fNumEntries and major/minor variables.
Definition RSampler.cxx:34
std::vector< std::size_t > fSamples
Definition RSampler.hxx:51
std::unique_ptr< RFlat2DMatrixOperators > fTensorOperators
Definition RSampler.hxx:53
void RandomUndersampler(RFlat2DMatrix &ShuffledTensor)
Undersample entries randomly from the majority dataset.
Definition RSampler.cxx:102
RSampler(const RFlat2DMatrix &dataset, std::vector< std::vector< std::size_t > > &rows, const std::string &sampleType, float sampleRatio, bool replacement=false, bool shuffle=true, std::size_t setSeed=0)
Definition RSampler.cxx:15
void Sampler(RFlat2DMatrix &SampledTensor)
Collection of sampling types.
Definition RSampler.cxx:46
Wrapper around ROOT::RVec<float> representing a 2D matrix.