Logo ROOT   6.12/07
Reference Guide
HyperParameterOptimisation.cxx
Go to the documentation of this file.
1 // @(#)root/tmva $Id$
2 // Author: Omar Zapata, Thomas James Stevenson.
3 
5 
6 #include "TMVA/Configurable.h"
7 #include "TMVA/DataSet.h"
8 #include "TMVA/Event.h"
9 #include "TMVA/MethodBase.h"
11 #include "TMVA/Types.h"
12 
13 #include "TGraph.h"
14 #include "TMultiGraph.h"
15 #include "TString.h"
16 #include "TSystem.h"
17 
18 #include <iostream>
19 #include <memory>
20 #include <vector>
21 
22 /*! \class TMVA::HyperParameterOptimisationResult
23 \ingroup TMVA
24 
25 */
26 
27 /*! \class TMVA::HyperParameterOptimisation
28 \ingroup TMVA
29 
30 */
31 
32 //_______________________________________________________________________
34  : fROCAVG(0.0), fROCCurves(std::make_shared<TMultiGraph>())
35 {
36 }
37 
38 //_______________________________________________________________________
40 {
41 }
42 
43 //_______________________________________________________________________
45 {
46 
47  return fROCCurves.get();
48 }
49 
50 //_______________________________________________________________________
52 {
55 
56  MsgLogger fLogger("HyperParameterOptimisation");
57 
58  for(UInt_t j=0; j<fFoldParameters.size(); ++j) {
59  fLogger<<kHEADER<< "===========================================================" << Endl;
60  fLogger<<kINFO<< "Optimisation for " << fMethodName << " fold " << j+1 << Endl;
61 
62  for(auto &it : fFoldParameters.at(j)) {
63  fLogger<<kINFO<< it.first << " " << it.second << Endl;
64  }
65  }
66 
68 
69 }
70 
71 //_______________________________________________________________________
73  fFomType("Separation"),
74  fFitType("Minuit"),
75  fNumFolds(5),
76  fResults(),
77  fClassifier(new TMVA::Factory("HyperParameterOptimisation","!V:!ROC:Silent:!ModelPersistence:!Color:!DrawProgressBar:AnalysisType=Classification"))
78 {
80 }
81 
82 //_______________________________________________________________________
84 {
85  fClassifier=nullptr;
86 }
87 
88 //_______________________________________________________________________
90 {
91  fNumFolds=i;
92  fDataLoader->MakeKFoldDataSet(fNumFolds);
94 }
95 
96 //_______________________________________________________________________
98 {
99  for (auto &meth : fMethods) {
100 
101  TString methodName = meth.GetValue<TString>("MethodName");
102  TString methodTitle = meth.GetValue<TString>("MethodTitle");
103  TString methodOptions = meth.GetValue<TString>("MethodOptions");
104 
105  if (!fFoldStatus) {
106  fDataLoader->MakeKFoldDataSet(fNumFolds);
107  fFoldStatus = kTRUE;
108  }
109  fResults.fMethodName = methodName;
110 
111  for (UInt_t i = 0; i < fNumFolds; ++i) {
112 
113  TString foldTitle = methodTitle;
114  foldTitle += "_opt";
115  foldTitle += i + 1;
116 
118  fDataLoader->PrepareFoldDataSet(i, TMVA::Types::kTraining);
119 
120  auto smethod = fClassifier->BookMethod(fDataLoader.get(), methodName, methodTitle, methodOptions);
121 
122  auto params = smethod->OptimizeTuningParameters(fFomType, fFitType);
123  fResults.fFoldParameters.push_back(params);
124 
125  smethod->Data()->DeleteResults(smethod->GetMethodName(), Types::kTraining, Types::kClassification);
126 
127  fClassifier->DeleteAllMethods();
128 
129  fClassifier->fMethodsMap.clear();
130  }
131  }
132 }
MsgLogger & Endl(MsgLogger &ml)
Definition: MsgLogger.h:158
Config & gConfig()
A TMultiGraph is a collection of TGraph (or derived) objects.
Definition: TMultiGraph.h:35
Basic string class.
Definition: TString.h:125
bool Bool_t
Definition: RtypesCore.h:59
HyperParameterOptimisationResult fResults
STL namespace.
static void SetIsTraining(Bool_t)
when this static function is called, it sets the flag whether events with negative event weight shoul...
Definition: Event.cxx:392
Abstract base class for all high level ml algorithms, you can book ml methods like BDT...
Definition: Envelope.h:43
virtual void Evaluate()
Virtual method to be implemented with your algorithm.
HyperParameterOptimisation(DataLoader *dataloader)
unsigned int UInt_t
Definition: RtypesCore.h:42
This is the main MVA steering class.
Definition: Factory.h:81
const Bool_t kFALSE
Definition: RtypesCore.h:88
std::shared_ptr< DataLoader > fDataLoader
Booked method information.
Definition: Envelope.h:47
ostringstream derivative to redirect and format output
Definition: MsgLogger.h:59
std::vector< std::map< TString, Double_t > > fFoldParameters
Abstract ClassifierFactory template that handles arbitrary types.
void SetSilent(Bool_t s)
Definition: Config.h:68
TMultiGraph * GetROCCurves(Bool_t fLegend=kTRUE)
static void EnableOutput()
Definition: MsgLogger.cxx:75
const Bool_t kTRUE
Definition: RtypesCore.h:87
std::vector< OptionMap > fMethods
Definition: Envelope.h:46