Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
VariableImportance.cxx
Go to the documentation of this file.
1// @(#)root/tmva $Id$
2// Author: Omar Zapata and Sergei Gleyzer
3
4/*! \class TMVA::VariableImportanceResult
5\ingroup TMVA
6*/
7
8/*! \class TMVA::VariableImportance
9\ingroup TMVA
10*/
11
13
14#include "TMVA/Config.h"
15#include "TMVA/DataSetInfo.h"
16#include "TMVA/Envelope.h"
17#include "TMVA/Factory.h"
18#include "TMVA/OptionMap.h"
19#include "TMVA/MethodBase.h"
20#include "TMVA/MethodCategory.h"
21#include "TMVA/MsgLogger.h"
22#include "TMVA/Types.h"
24
25#include "TAxis.h"
26#include "TCanvas.h"
27#include "TH1.h"
28#include "TRandom3.h"
29#include "TStyle.h"
30
31#include <bitset>
32#include <memory>
33#include <utility>
34
35
36//number of bits for bitset
37#define NBITS 32
38
39////////////////////////////////////////////////////////////////////////////////
40
41TMVA::VariableImportanceResult::VariableImportanceResult():fImportanceValues("VariableImportance"),
42 fImportanceHist(nullptr)
43{
44
45}
46
47////////////////////////////////////////////////////////////////////////////////
48
50{
51 fImportanceValues = obj.fImportanceValues;
52 fImportanceHist = obj.fImportanceHist;
53}
54
55////////////////////////////////////////////////////////////////////////////////
56
58{
61
62 MsgLogger fLogger("VariableImportance");
63 if(fType==VIType::kShort)
64 {
65 fLogger<<kINFO<<"Variable Importance Results (Short)"<<Endl;
66 }else if(fType==VIType::kAll)
67 {
68 fLogger<<kINFO<<"Variable Importance Results (All)"<<Endl;
69 }else{
70 fLogger<<kINFO<<"Variable Importance Results (Random)"<<Endl;
71 }
72
73 fImportanceValues.Print();
75}
76
77////////////////////////////////////////////////////////////////////////////////
78
80{
81 TCanvas *c=new TCanvas(name.Data());
82 fImportanceHist->Draw("");
83 fImportanceHist->GetXaxis()->SetTitle(" Variable Names ");
84 fImportanceHist->GetYaxis()->SetTitle(" Importance (%) ");
85 c->Draw();
86 return c;
87}
88
89////////////////////////////////////////////////////////////////////////////////
90
91TMVA::VariableImportance::VariableImportance(TMVA::DataLoader *dataloader):TMVA::Envelope("VariableImportance",dataloader,nullptr),fType(VIType::kShort)
92{
93 fClassifier=std::unique_ptr<Factory>(new TMVA::Factory("VariableImportance","!V:!ROC:!ModelPersistence:Silent:Color:!DrawProgressBar:AnalysisType=Classification"));
94}
95
96////////////////////////////////////////////////////////////////////////////////
97
99{
100 fClassifier=nullptr;
101}
102
103////////////////////////////////////////////////////////////////////////////////
104
106{
107
108 //NOTE: Put the type of VI Algorithm in the results Print
109 if(fType==VIType::kShort)
110 {
111 EvaluateImportanceShort();
112 }else if(fType==VIType::kAll)
113 {
114 EvaluateImportanceAll();
115 }else{
116 UInt_t nbits=fDataLoader->GetDefaultDataSetInfo().GetNVariables();
117 if(nbits<10)
118 Log()<<kERROR<<"Running variable importance with less that 10 varibales in Random mode "<<
119 "can to produce inconsisten results"<<Endl;
120 EvaluateImportanceRandom(pow(nbits,2));
121 }
122 fResults.fType = fType;
125 Log()<<kINFO<<"Evaluation done."<<Endl;
127}
128
129////////////////////////////////////////////////////////////////////////////////
130
132{
133 ULong_t sum=0;
134 for(ULong_t n=0;n<i;n++) sum+=pow(2,n);
135 return sum;
136}
137
138////////////////////////////////////////////////////////////////////////////////
139
140TH1F* TMVA::VariableImportance::GetImportance(const UInt_t nbits,std::vector<Float_t> &importances,std::vector<TString> &varNames)
141{
142 TH1F *vihist = new TH1F("vihist", "", nbits, 0, nbits);
143
144 gStyle->SetOptStat(000000);
145
146 Float_t normalization = 0.0;
147 for (UInt_t i = 0; i < nbits; i++) normalization += importances[i];
148
149 Float_t roc = 0.0;
150
153
154
155 for (UInt_t i = 1; i < nbits + 1; i++) {
156 roc = 100.0 * importances[i - 1] / normalization;
157 vihist->GetXaxis()->SetBinLabel(i, varNames[i - 1].Data());
158 vihist->SetBinContent(i, roc);
159 }
160
161 vihist->LabelsOption("v >", "X");
162 vihist->SetBarWidth(0.97);
163 vihist->SetFillColor(TColor::GetColor("#006600"));
164
165 vihist->GetXaxis()->SetTitle(" Variable Names ");
166 vihist->GetXaxis()->SetTitleSize(0.045);
167 vihist->GetXaxis()->CenterTitle();
168 vihist->GetXaxis()->SetTitleOffset(1.24);
169
170 vihist->GetYaxis()->SetTitle(" Importance (%)");
171 vihist->GetYaxis()->SetTitleSize(0.045);
172 vihist->GetYaxis()->CenterTitle();
173 vihist->GetYaxis()->SetTitleOffset(1.24);
174
175 vihist->GetYaxis()->SetRangeUser(-7, 50);
176 vihist->SetDirectory(0);
177
178 return vihist;
179}
180
181////////////////////////////////////////////////////////////////////////////////
182
184{
185 for (auto &meth : fMethods) {
186 TString methodName = meth.GetValue<TString>("MethodName");
187 TString methodTitle = meth.GetValue<TString>("MethodTitle");
188 TString methodOptions = meth.GetValue<TString>("MethodOptions");
189
190 uint32_t x = 0;
191 uint32_t y = 0;
192 // getting number of variables and variable names from loader
193 const UInt_t nbits = fDataLoader->GetDefaultDataSetInfo().GetNVariables();
194 std::vector<TString> varNames = fDataLoader->GetDefaultDataSetInfo().GetListOfVariables();
195
196 ULong_t range = Sum(nbits);
197
198 // vector to save importances
199 std::vector<Float_t> importances(nbits);
200 for (UInt_t i = 0; i < nbits; i++)
201 importances[i] = 0;
202
203 Float_t SROC, SSROC; // computed ROC value for every Seed and SubSeed
204
205 x = range;
206
207 std::bitset<NBITS> xbitset(x);
208 if (x == 0)
209 Log() << kFATAL << "Error: need at least one variable."; // dataloader need at least one variable
210
211 // creating loader for seed
212 TMVA::DataLoader *seeddl = new TMVA::DataLoader(xbitset.to_string());
213
214 // adding variables from seed
215 for (UInt_t index = 0; index < nbits; index++) {
216 if (xbitset[index])
217 seeddl->AddVariable(varNames[index], 'F');
218 }
219
220 // Loading Dataset
221 DataLoaderCopy(seeddl, fDataLoader.get());
222
223 // Booking Seed
224 fClassifier->BookMethod(seeddl, methodName, methodTitle, methodOptions);
225
226 // Train/Test/Evaluation
227 fClassifier->TrainAllMethods();
228 fClassifier->TestAllMethods();
229 fClassifier->EvaluateAllMethods();
230
231 // getting ROC
232 SROC = fClassifier->GetROCIntegral(xbitset.to_string(), methodTitle);
233
234 delete seeddl;
235 fClassifier->DeleteAllMethods();
236 fClassifier->fMethodsMap.clear();
237
238 for (uint32_t i = 0; i < NBITS; ++i) {
239 if (x & (1 << i)) {
240 y = x & ~(1 << i);
241 std::bitset<NBITS> ybitset(y);
242 //need at least one variable
243 //NOTE: if subssed is zero then is the special case
244 //that count in xbitset is 1
245 Double_t ny = log(x - y) / 0.693147;
246 if (y == 0) {
247 importances[ny] = SROC - 0.5;
248 continue;
249 }
250
251 //creating loader for subseed
252 TMVA::DataLoader *subseeddl = new TMVA::DataLoader(ybitset.to_string());
253 //adding variables from subseed
254 for (UInt_t index = 0; index < nbits; index++) {
255 if (ybitset[index]) subseeddl->AddVariable(varNames[index], 'F');
256 }
257
258 //Loading Dataset
259 DataLoaderCopy(subseeddl,fDataLoader.get());
260
261 //Booking SubSeed
262 fClassifier->BookMethod(subseeddl, methodName, methodTitle, methodOptions);
263
264 //Train/Test/Evaluation
265 fClassifier->TrainAllMethods();
266 fClassifier->TestAllMethods();
267 fClassifier->EvaluateAllMethods();
268
269 //getting ROC
270 SSROC = fClassifier->GetROCIntegral(ybitset.to_string(), methodTitle);
271 importances[ny] += SROC - SSROC;
272
273 delete subseeddl;
274 fClassifier->DeleteAllMethods();
275 fClassifier->fMethodsMap.clear();
276 }
277 }
278 Float_t normalization = 0.0;
279 for (UInt_t i = 0; i < nbits; i++) normalization += importances[i];
280
281 for(UInt_t i=0;i<nbits;i++){
282 //adding values
283 fResults.fImportanceValues[varNames[i]]=(100.0 * importances[i] / normalization);
284 //adding sufix
285 fResults.fImportanceValues[varNames[i]]=fResults.fImportanceValues.GetValue<TString>(varNames[i])+" % ";
286 }
287 fResults.fImportanceHist = std::shared_ptr<TH1F>(GetImportance(nbits,importances,varNames));
288 }
289}
290
291////////////////////////////////////////////////////////////////////////////////
292
294{
295 for (auto &meth : fMethods) {
296
297 TString methodName = meth.GetValue<TString>("MethodName");
298 TString methodTitle = meth.GetValue<TString>("MethodTitle");
299 TString methodOptions = meth.GetValue<TString>("MethodOptions");
300
301 TRandom3 *rangen = new TRandom3(0); // Random Gen.
302
303 uint32_t x = 0;
304 uint32_t y = 0;
305
306 // getting number of variables and variable names from loader
307 const UInt_t nbits = fDataLoader->GetDefaultDataSetInfo().GetNVariables();
308 std::vector<TString> varNames = fDataLoader->GetDefaultDataSetInfo().GetListOfVariables();
309
310 ULong_t range = pow(2, nbits);
311
312 // vector to save importances
313 std::vector<Float_t> importances(nbits);
314 Float_t importances_norm = 0;
315
316 for (UInt_t i = 0; i < nbits; i++)
317 importances[i] = 0;
318
319 Float_t SROC, SSROC; // computed ROC value for every Seed and SubSeed
320
321 x = range;
322
323 for (UInt_t n = 0; n < seeds; n++) {
324 x = rangen->Integer(range);
325
326 std::bitset<NBITS> xbitset(x);
327 if (x == 0)
328 continue; // dataloader need at least one variable
329
330 // creating loader for seed
331 TMVA::DataLoader *seeddl = new TMVA::DataLoader(xbitset.to_string());
332
333 // adding variables from seed
334 for (UInt_t index = 0; index < nbits; index++) {
335 if (xbitset[index]) seeddl->AddVariable(varNames[index], 'F');
336 }
337
338 //Loading Dataset
339 DataLoaderCopy(seeddl,fDataLoader.get());
340
341 //Booking Seed
342 fClassifier->BookMethod(seeddl, methodName, methodTitle, methodOptions);
343
344 //Train/Test/Evaluation
345 fClassifier->TrainAllMethods();
346 fClassifier->TestAllMethods();
347 fClassifier->EvaluateAllMethods();
348
349 //getting ROC
350 SROC = fClassifier->GetROCIntegral(xbitset.to_string(), methodTitle);
351
352 delete seeddl;
353 fClassifier->DeleteAllMethods();
354 fClassifier->fMethodsMap.clear();
355
356 for (uint32_t i = 0; i < 32; ++i) {
357 if (x & (1 << i)) {
358 y = x & ~(1 << i);
359 std::bitset<NBITS> ybitset(y);
360 //need at least one variable
361 //NOTE: if subssed is zero then is the special case
362 //that count in xbitset is 1
363 Double_t ny = log(x - y) / 0.693147;
364 if (y == 0) {
365 importances[ny] = SROC - 0.5;
366 importances_norm += importances[ny];
367 continue;
368 }
369
370 //creating loader for subseed
371 TMVA::DataLoader *subseeddl = new TMVA::DataLoader(ybitset.to_string());
372 //adding variables from subseed
373 for (UInt_t index = 0; index < nbits; index++) {
374 if (ybitset[index]) subseeddl->AddVariable(varNames[index], 'F');
375 }
376
377 //Loading Dataset
378 DataLoaderCopy(subseeddl,fDataLoader.get());
379
380 //Booking SubSeed
381 fClassifier->BookMethod(subseeddl, methodName, methodTitle, methodOptions);
382
383 //Train/Test/Evaluation
384 fClassifier->TrainAllMethods();
385 fClassifier->TestAllMethods();
386 fClassifier->EvaluateAllMethods();
387
388 //getting ROC
389 SSROC = fClassifier->GetROCIntegral(ybitset.to_string(), methodTitle);
390 importances[ny] += SROC - SSROC;
391
392 delete subseeddl;
393 fClassifier->DeleteAllMethods();
394 fClassifier->fMethodsMap.clear();
395 }
396 }
397 }
398
399 Float_t normalization = 0.0;
400 for (UInt_t i = 0; i < nbits; i++) normalization += importances[i];
401
402 for(UInt_t i=0;i<nbits;i++){
403 //adding values
404 fResults.fImportanceValues[varNames[i]]=(100.0 * importances[i] / normalization);
405 //adding sufix
406 fResults.fImportanceValues[varNames[i]]=fResults.fImportanceValues.GetValue<TString>(varNames[i])+" % ";
407 }
408 fResults.fImportanceHist = std::shared_ptr<TH1F>(GetImportance(nbits,importances,varNames));
409 delete rangen;
410 }
411}
412
413////////////////////////////////////////////////////////////////////////////////
414
416{
417 for (auto &meth : fMethods) {
418 TString methodName = meth.GetValue<TString>("MethodName");
419 TString methodTitle = meth.GetValue<TString>("MethodTitle");
420 TString methodOptions = meth.GetValue<TString>("MethodOptions");
421
422 uint32_t x = 0;
423 uint32_t y = 0;
424
425 // getting number of variables and variable names from loader
426 const UInt_t nbits = fDataLoader->GetDefaultDataSetInfo().GetNVariables();
427 std::vector<TString> varNames = fDataLoader->GetDefaultDataSetInfo().GetListOfVariables();
428
429 ULong_t range = pow(2, nbits);
430
431 // vector to save importances
432 std::vector<Float_t> importances(nbits);
433
434 // vector to save ROC-Integral values
435 std::vector<Float_t> ROC(range);
436 ROC[0] = 0.5;
437 for (UInt_t i = 0; i < nbits; i++)
438 importances[i] = 0;
439
440 Float_t SROC, SSROC; // computed ROC value
441 for (x = 1; x < range; x++) {
442
443 std::bitset<NBITS> xbitset(x);
444 if (x == 0)
445 continue; // dataloader need at least one variable
446
447 // creating loader for seed
448 TMVA::DataLoader *seeddl = new TMVA::DataLoader(xbitset.to_string());
449
450 // adding variables from seed
451 for (UInt_t index = 0; index < nbits; index++) {
452 if (xbitset[index]) seeddl->AddVariable(varNames[index], 'F');
453 }
454
455 DataLoaderCopy(seeddl,fDataLoader.get());
456
457 seeddl->PrepareTrainingAndTestTree(fDataLoader->GetDefaultDataSetInfo().GetCut("Signal"), fDataLoader->GetDefaultDataSetInfo().GetCut("Background"), fDataLoader->GetDefaultDataSetInfo().GetSplitOptions());
458
459 //Booking Seed
460 fClassifier->BookMethod(seeddl, methodName, methodTitle, methodOptions);
461
462 //Train/Test/Evaluation
463 fClassifier->TrainAllMethods();
464 fClassifier->TestAllMethods();
465 fClassifier->EvaluateAllMethods();
466
467 //getting ROC
468 ROC[x] = fClassifier->GetROCIntegral(xbitset.to_string(), methodTitle);
469
470 delete seeddl;
471 fClassifier->DeleteAllMethods();
472 fClassifier->fMethodsMap.clear();
473 }
474
475
476 for ( x = 0; x <range ; x++)
477 {
478 SROC=ROC[x];
479 for (uint32_t i = 0; i < NBITS; ++i) {
480 if (x & (1 << i)) {
481 y = x & ~(1 << i);
482 std::bitset<NBITS> ybitset(y);
483
484 Float_t ny = log(x - y) / 0.693147;
485 if (y == 0) {
486 importances[ny] = SROC - 0.5;
487 continue;
488 }
489
490 //getting ROC
491 SSROC = ROC[y];
492 importances[ny] += SROC - SSROC;
493 }
494
495 }
496 }
497 Float_t normalization = 0.0;
498 for (UInt_t i = 0; i < nbits; i++) normalization += importances[i];
499
500 for(UInt_t i=0;i<nbits;i++){
501 //adding values
502 fResults.fImportanceValues[varNames[i]]=(100.0 * importances[i] / normalization);
503 //adding sufix
504 fResults.fImportanceValues[varNames[i]]=fResults.fImportanceValues.GetValue<TString>(varNames[i])+" % ";
505 }
506 fResults.fImportanceHist = std::shared_ptr<TH1F>(GetImportance(nbits,importances,varNames));
507 }
508}
#define c(i)
Definition RSha256.hxx:101
const Bool_t kFALSE
Definition RtypesCore.h:92
unsigned long ULong_t
Definition RtypesCore.h:55
double Double_t
Definition RtypesCore.h:59
float Float_t
Definition RtypesCore.h:57
const Bool_t kTRUE
Definition RtypesCore.h:91
char name[80]
Definition TGX11.cxx:110
double pow(double, double)
double log(double)
R__EXTERN TStyle * gStyle
Definition TStyle.h:412
#define NBITS
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition TAttAxis.cxx:293
virtual void SetTitleSize(Float_t size=0.04)
Set size of axis title.
Definition TAttAxis.cxx:303
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition TAttFill.h:37
virtual void SetBinLabel(Int_t bin, const char *label)
Set label for bin.
Definition TAxis.cxx:823
void CenterTitle(Bool_t center=kTRUE)
Center axis title.
Definition TAxis.h:184
virtual void SetRangeUser(Double_t ufirst, Double_t ulast)
Set the viewing range for the axis from ufirst to ulast (in user coordinates, that is,...
Definition TAxis.cxx:946
The Canvas class.
Definition TCanvas.h:23
static Int_t GetColor(const char *hexcolor)
Static method returning color number for color specified by hex color string of form: "#rrggbb",...
Definition TColor.cxx:1769
1-D histogram with a float per channel (see TH1 documentation)}
Definition TH1.h:575
virtual void SetDirectory(TDirectory *dir)
By default when an histogram is created, it is added to the list of histogram objects in the current ...
Definition TH1.cxx:8777
virtual void LabelsOption(Option_t *option="h", Option_t *axis="X")
Sort bins with labels or set option(s) to draw axis with labels.
Definition TH1.cxx:5313
TAxis * GetXaxis()
Get the behaviour adopted by the object about the statoverflows. See EStatOverflows for more informat...
Definition TH1.h:320
TAxis * GetYaxis()
Definition TH1.h:321
virtual void SetBinContent(Int_t bin, Double_t content)
Set bin content see convention for numbering bins in TH1::GetBin In case the bin number is greater th...
Definition TH1.cxx:9062
virtual void SetBarWidth(Float_t width=0.5)
Set the width of bars as fraction of the bin width for drawing mode "B".
Definition TH1.h:360
void SetSilent(Bool_t s)
Definition Config.h:65
void PrepareTrainingAndTestTree(const TCut &cut, const TString &splitOpt)
prepare the training and test trees -> same cuts for signal and background
void AddVariable(const TString &expression, const TString &title, const TString &unit, char type='F', Double_t min=0, Double_t max=0)
user inserts discriminating variable in data set info
Abstract base class for all high level ml algorithms, you can book ml methods like BDT,...
Definition Envelope.h:44
This is the main MVA steering class.
Definition Factory.h:80
ostringstream derivative to redirect and format output
Definition MsgLogger.h:59
static void EnableOutput()
Definition MsgLogger.cxx:74
std::shared_ptr< TH1F > fImportanceHist
TCanvas * Draw(const TString name="VariableImportance") const
std::unique_ptr< Factory > fClassifier
virtual void Evaluate()
Virtual method to be implemented with your algorithm.
void EvaluateImportanceRandom(UInt_t nseeds)
VariableImportance(DataLoader *loader)
TH1F * GetImportance(const UInt_t nbits, std::vector< Float_t > &importances, std::vector< TString > &varNames)
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition TNamed.cxx:164
virtual void Print(Option_t *option="") const
This method must be overridden when a class wants to print itself.
Definition TObject.cxx:552
Random number generator class based on M.
Definition TRandom3.h:27
Basic string class.
Definition TString.h:136
void SetOptStat(Int_t stat=1)
The type of information printed in the histogram statistics box can be selected via the parameter mod...
Definition TStyle.cxx:1589
void SetTitleXOffset(Float_t offset=1)
Definition TStyle.h:392
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16
create variable transformations
void DataLoaderCopy(TMVA::DataLoader *des, TMVA::DataLoader *src)
Config & gConfig()
MsgLogger & Endl(MsgLogger &ml)
Definition MsgLogger.h:158
static uint64_t sum(uint64_t i)
Definition Factory.cxx:2345