60 Log() << kFATAL <<
"DataSet prepared for \"" <<
fNumFolds <<
"\" folds, requested fold \"" << foldNumber
61 <<
"\" is outside of range." <<
Endl;
65 auto prepareDataSetInternal = [
this, &dsi, foldNumber](std::vector<std::vector<Event *>>
vec) {
69 UInt_t nTotal = std::accumulate(
vec.begin(),
vec.end(), 0,
70 [&](
UInt_t sum, std::vector<TMVA::Event *>
v) { return sum + v.size(); });
72 UInt_t nTrain = nTotal -
vec.at(foldNumber).size();
75 std::vector<Event *> tempTrain;
76 std::vector<Event *> tempTest;
78 tempTrain.reserve(nTrain);
79 tempTest.reserve(nTest);
83 if (
i == foldNumber) {
87 tempTrain.insert(tempTrain.end(),
vec.at(
i).begin(),
vec.at(
i).end());
91 tempTest.insert(tempTest.end(),
vec.at(foldNumber).begin(),
vec.at(foldNumber).end());
93 Log() << kDEBUG <<
"Fold prepared, num events in training set: " << tempTrain.size() <<
Endl;
94 Log() << kDEBUG <<
"Fold prepared, num events in test set: " << tempTest.size() <<
Endl;
106 Log() << kFATAL <<
"PrepareFoldDataSet can only work with training and testing data sets." << std::endl;
117 Log() << kFATAL <<
"Only kTraining is supported for CvSplit::RecombineKFoldDataSet currently." << std::endl;
120 std::vector<Event *> *tempVec =
new std::vector<Event *>;
144 throw std::runtime_error(
"Split expression \"" + std::string(fSplitExpr.Data()) +
"\" is not a valid TFormula.");
152 if (
name ==
"NumFolds" ||
name ==
"numFolds") {
167 auto iFormulaPar = p.first;
168 auto iSpectator = p.second;
183 throw std::runtime_error(
"Output of splitExpr must be non-negative.");
186 UInt_t iFold = std::lround(iFold_d);
187 if (iFold >= numFolds) {
188 throw std::runtime_error(
"Output of splitExpr should be a non-negative"
189 "integer between 0 and numFolds-1 inclusive.");
210 for (
UInt_t iSpectator = 0; iSpectator < spectatorInfos.size(); ++iSpectator) {
221 throw std::runtime_error(
"Spectator \"" + std::string(
name.Data()) +
"\" not found.");
266 Log() << kINFO <<
"Splitting in k-folds has been already done" <<
Endl;
297 std::vector<UInt_t> fOrigToFoldMapping;
298 fOrigToFoldMapping.reserve(nEntries);
300 for (
UInt_t iEvent = 0; iEvent < nEntries; ++iEvent) {
301 fOrigToFoldMapping.push_back(iEvent % numFolds);
306 std::shuffle(fOrigToFoldMapping.begin(), fOrigToFoldMapping.end(), rng);
308 return fOrigToFoldMapping;
319std::vector<std::vector<TMVA::Event *>>
322 const ULong64_t nEntries = oldSet.size();
323 const ULong64_t foldSize = nEntries / numFolds;
325 std::vector<std::vector<Event *>> tempSets;
327 for (
UInt_t iFold = 0; iFold < numFolds; ++iFold) {
328 tempSets.emplace_back();
329 tempSets.at(iFold).reserve(foldSize);
339 tempSets.at((
UInt_t)iFold).push_back(ev);
344 std::vector<UInt_t> fOrigToFoldMapping;
347 for (
UInt_t iEvent = 0; iEvent < nEntries; ++iEvent) {
348 UInt_t iFold = fOrigToFoldMapping[iEvent];
350 tempSets.at(iFold).push_back(ev);
356 std::vector<std::vector<TMVA::Event *>> oldSets;
357 oldSets.reserve(numClasses);
359 for(
UInt_t iClass = 0; iClass < numClasses; iClass++){
360 oldSets.emplace_back();
362 oldSets.reserve(nEntries);
365 for(
UInt_t iEvent = 0; iEvent < nEntries; ++iEvent){
369 oldSets.at(iClass).push_back(ev);
375 std::shuffle(oldSets.at(
i).begin(), oldSets.at(
i).end(), rng);
379 std::vector<UInt_t> fOrigToFoldMapping;
382 for (
UInt_t iEvent = 0; iEvent < oldSets.at(
i).
size(); ++iEvent) {
383 UInt_t iFold = fOrigToFoldMapping[iEvent];
385 tempSets.at(iFold).push_back(ev);
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
unsigned long long ULong64_t
Int_t fIdxFormulaParNumFolds
Maps parameter indicies in splitExpr to their spectator index in the datasetinfo.
UInt_t Eval(UInt_t numFolds, const Event *ev)
std::vector< std::pair< Int_t, Int_t > > fFormulaParIdxToDsiSpecIdx
UInt_t GetSpectatorIndexForName(DataSetInfo &dsi, TString name)
static Bool_t Validate(TString expr)
std::vector< Double_t > fParValues
TFormula for splitExpr.
CvSplitKFoldsExpr(DataSetInfo &dsi, TString expr)
TFormula fSplitFormula
Expression used to split data into folds. Should output values between 0 and numFolds.
Bool_t fStratified
If true, use stratified split. (Balance class presence in each fold).
std::vector< UInt_t > GetEventIndexToFoldMapping(UInt_t nEntries, UInt_t numFolds, UInt_t seed=100)
Generates a vector of fold assignments.
std::unique_ptr< CvSplitKFoldsExpr > fSplitExpr
std::map< const TMVA::Event *, UInt_t > fEventToFoldMapping
void MakeKFoldDataSet(DataSetInfo &dsi) override
Prepares a DataSet for cross validation.
std::vector< std::vector< Event * > > SplitSets(std::vector< TMVA::Event * > &oldSet, UInt_t numFolds, UInt_t numClasses)
Split sets for into k-folds.
TString fSplitExprString
! Expression used to split data into folds. Should output values between 0 and numFolds.
CvSplitKFolds(UInt_t numFolds, TString splitExpr="", Bool_t stratified=kTRUE, UInt_t seed=100)
Splits a dataset into k folds, ready for use in cross validation.
virtual void RecombineKFoldDataSet(DataSetInfo &dsi, Types::ETreeType tt=Types::kTraining)
std::vector< std::vector< TMVA::Event * > > fTestEvents
std::vector< std::vector< TMVA::Event * > > fTrainEvents
virtual void PrepareFoldDataSet(DataSetInfo &dsi, UInt_t foldNumber, Types::ETreeType tt)
Set training and test set vectors of dataset described by dsi.
Class that contains all the data information.
std::vector< VariableInfo > & GetSpectatorInfos()
UInt_t GetNClasses() const
DataSet * GetDataSet() const
returns data set
void SetEventCollection(std::vector< Event * > *, Types::ETreeType, Bool_t deleteEvents=true)
Sets the event collection (by DataSetFactory)
const std::vector< Event * > & GetEventCollection(Types::ETreeType type=Types::kMaxTreeType) const
Float_t GetSpectator(UInt_t ivar) const
return spectator content
Class for type info of MVA input variable.
const TString & GetLabel() const
const TString & GetExpression() const
const char * GetName() const override
Returns name of object.
MsgLogger & Endl(MsgLogger &ml)
static uint64_t sum(uint64_t i)