Logo ROOT   6.07/09
Reference Guide
VariableImportance.cxx
Go to the documentation of this file.
1 // @(#)root/tmva $Id$
2 // Author: Omar Zapata and Sergei Gleyzer
3 
5 
6 #include "TMVA/Config.h"
7 #include "TMVA/DataSetInfo.h"
8 #include "TMVA/Envelope.h"
9 #include "TMVA/Factory.h"
10 #include "TMVA/OptionMap.h"
11 #include "TMVA/MethodBase.h"
12 #include "TMVA/MethodCategory.h"
13 #include "TMVA/MsgLogger.h"
14 #include "TMVA/Types.h"
16 
17 #include "TAxis.h"
18 #include "TGraph.h"
19 #include "TCanvas.h"
20 #include "TH1.h"
21 #include "TRandom3.h"
22 #include "TStyle.h"
23 #include "TSystem.h"
24 
25 #include <bitset>
26 #include <iostream>
27 #include <memory>
28 #include <utility>
29 
30 
31 //number of bits for bitset
32 #define NBITS 32
33 
34 TMVA::VariableImportanceResult::VariableImportanceResult():fImportanceValues("VariableImportance"),
35  fImportanceHist(nullptr)
36 {
37 
38 }
39 
41 {
44 }
45 
46 
48 {
51 
52  MsgLogger fLogger("VariableImportance");
54  {
55  fLogger<<kINFO<<"Variable Importance Results (Short)"<<Endl;
56  }else if(fType==VIType::kAll)
57  {
58  fLogger<<kINFO<<"Variable Importance Results (All)"<<Endl;
59  }else{
60  fLogger<<kINFO<<"Variable Importance Results (Random)"<<Endl;
61  }
62 
65 }
66 
67 
69 {
70  TCanvas *c=new TCanvas(name.Data());
71  fImportanceHist->Draw("");
72  fImportanceHist->GetXaxis()->SetTitle(" Variable Names ");
73  fImportanceHist->GetYaxis()->SetTitle(" Importance (%) ");
74  c->Draw();
75  return c;
76 }
77 
79 {
80  fClassifier=std::unique_ptr<Factory>(new TMVA::Factory("VariableImportance","!V:!ROC:!ModelPersistence:Silent:Color:!DrawProgressBar:AnalysisType=Classification"));
81 }
82 
84 {
85  fClassifier=nullptr;
86 }
87 
88 
90 {
91  TString methodName = fMethod.GetValue<TString>("MethodName");
92  TString methodTitle = fMethod.GetValue<TString>("MethodTitle");
93  TString methodOptions = fMethod.GetValue<TString>("MethodOptions");
94 
95  //NOTE: Put the type of VI Algorithm in the results Print
97  {
99  }else if(fType==VIType::kAll)
100  {
102  }else{
103  UInt_t nbits=fDataLoader->GetDefaultDataSetInfo().GetNVariables();
104  if(nbits<10)
105  Log()<<kERROR<<"Running variable importance with less that 10 varibales in Random mode "<<
106  "can to produce inconsisten results"<<Endl;
107  EvaluateImportanceRandom(pow(2,nbits));
108  }
109  fResults.fType = fType;
112  Log()<<kINFO<<"Evaluation done."<<Endl;
114 }
115 
117 {
118  ULong_t sum=0;
119  for(ULong_t n=0;n<i;n++) sum+=pow(2,n);
120  return sum;
121 }
122 
123 TH1F* TMVA::VariableImportance::GetImportance(const UInt_t nbits,std::vector<Float_t> &importances,std::vector<TString> &varNames)
124 {
125  TH1F *vihist = new TH1F("vihist", "", nbits, 0, nbits);
126 
127  gStyle->SetOptStat(000000);
128 
129  Float_t normalization = 0.0;
130  for (UInt_t i = 0; i < nbits; i++) normalization += importances[i];
131 
132  Float_t roc = 0.0;
133 
134  gStyle->SetTitleXOffset(0.4);
135  gStyle->SetTitleXOffset(1.2);
136 
137 
138  for (UInt_t i = 1; i < nbits + 1; i++) {
139  roc = 100.0 * importances[i - 1] / normalization;
140  vihist->GetXaxis()->SetBinLabel(i, varNames[i - 1].Data());
141  vihist->SetBinContent(i, roc);
142  }
143 
144  vihist->LabelsOption("v >", "X");
145  vihist->SetBarWidth(0.97);
146  vihist->SetFillColor(TColor::GetColor("#006600"));
147 
148  vihist->GetXaxis()->SetTitle(" Variable Names ");
149  vihist->GetXaxis()->SetTitleSize(0.045);
150  vihist->GetXaxis()->CenterTitle();
151  vihist->GetXaxis()->SetTitleOffset(1.24);
152 
153  vihist->GetYaxis()->SetTitle(" Importance (%)");
154  vihist->GetYaxis()->SetTitleSize(0.045);
155  vihist->GetYaxis()->CenterTitle();
156  vihist->GetYaxis()->SetTitleOffset(1.24);
157 
158  vihist->GetYaxis()->SetRangeUser(-7, 50);
159  vihist->SetDirectory(0);
160 
161  return vihist;
162 }
163 
165 {
166  TString methodName = fMethod.GetValue<TString>("MethodName");
167  TString methodTitle = fMethod.GetValue<TString>("MethodTitle");
168  TString methodOptions = fMethod.GetValue<TString>("MethodOptions");
169 
170  uint32_t x = 0;
171  uint32_t y = 0;
172  //getting number of variables and variable names from loader
173  const UInt_t nbits = fDataLoader->GetDefaultDataSetInfo().GetNVariables();
174  std::vector<TString> varNames = fDataLoader->GetDefaultDataSetInfo().GetListOfVariables();
175 
176  ULong_t range = Sum(nbits);
177 
178  //vector to save importances
179  std::vector<Float_t> importances(nbits);
180  for (UInt_t i = 0; i < nbits; i++)importances[i] = 0;
181 
182  Float_t SROC, SSROC; //computed ROC value for every Seed and SubSeed
183 
184  x = range;
185 
186  std::bitset<NBITS> xbitset(x);
187  if (x == 0) Log()<<kFATAL<<"Error: need at least one variable."; //dataloader need at least one variable
188 
189 
190  //creating loader for seed
191  TMVA::DataLoader *seeddl = new TMVA::DataLoader(xbitset.to_string());
192 
193  //adding variables from seed
194  for (UInt_t index = 0; index < nbits; index++){
195  if (xbitset[index]) seeddl->AddVariable(varNames[index], 'F');
196  }
197 
198  //Loading Dataset
199  DataLoaderCopy(seeddl,fDataLoader.get());
200 
201  //Booking Seed
202  fClassifier->BookMethod(seeddl, methodName, methodTitle, methodOptions);
203 
204  //Train/Test/Evaluation
205  fClassifier->TrainAllMethods();
206  fClassifier->TestAllMethods();
207  fClassifier->EvaluateAllMethods();
208 
209  //getting ROC
210  SROC = fClassifier->GetROCIntegral(xbitset.to_string(), methodTitle);
211 
212  delete seeddl;
213  fClassifier->DeleteAllMethods();
214 
215  for (uint32_t i = 0; i < NBITS; ++i) {
216  if (x & (1 << i)) {
217  y = x & ~(1 << i);
218  std::bitset<NBITS> ybitset(y);
219  //need at least one variable
220  //NOTE: if subssed is zero then is the special case
221  //that count in xbitset is 1
222  Double_t ny = log(x - y) / 0.693147;
223  if (y == 0) {
224  importances[ny] = SROC - 0.5;
225  continue;
226  }
227 
228  //creating loader for subseed
229  TMVA::DataLoader *subseeddl = new TMVA::DataLoader(ybitset.to_string());
230  //adding variables from subseed
231  for (UInt_t index = 0; index < nbits; index++) {
232  if (ybitset[index]) subseeddl->AddVariable(varNames[index], 'F');
233  }
234 
235  //Loading Dataset
236  DataLoaderCopy(subseeddl,fDataLoader.get());
237 
238  //Booking SubSeed
239  fClassifier->BookMethod(subseeddl, methodName, methodTitle, methodOptions);
240 
241  //Train/Test/Evaluation
242  fClassifier->TrainAllMethods();
243  fClassifier->TestAllMethods();
244  fClassifier->EvaluateAllMethods();
245 
246  //getting ROC
247  SSROC = fClassifier->GetROCIntegral(ybitset.to_string(), methodTitle);
248  importances[ny] += SROC - SSROC;
249 
250  delete subseeddl;
251  fClassifier->DeleteAllMethods();
252  }
253  }
254  Float_t normalization = 0.0;
255  for (UInt_t i = 0; i < nbits; i++) normalization += importances[i];
256 
257  for(UInt_t i=0;i<nbits;i++){
258  //adding values
259  fResults.fImportanceValues[varNames[i]]=(100.0 * importances[i] / normalization);
260  //adding sufix
261  fResults.fImportanceValues[varNames[i]]=fResults.fImportanceValues.GetValue<TString>(varNames[i])+" % ";
262  }
263  fResults.fImportanceHist = std::shared_ptr<TH1F>(GetImportance(nbits,importances,varNames));
264 }
265 
267 {
268  TString methodName = fMethod.GetValue<TString>("MethodName");
269  TString methodTitle = fMethod.GetValue<TString>("MethodTitle");
270  TString methodOptions = fMethod.GetValue<TString>("MethodOptions");
271 
272  TRandom3 *rangen = new TRandom3(0); //Random Gen.
273 
274  uint32_t x = 0;
275  uint32_t y = 0;
276 
277  //getting number of variables and variable names from loader
278  const UInt_t nbits = fDataLoader->GetDefaultDataSetInfo().GetNVariables();
279  std::vector<TString> varNames = fDataLoader->GetDefaultDataSetInfo().GetListOfVariables();
280 
281  ULong_t range = pow(2, nbits);
282 
283  //vector to save importances
284  std::vector<Float_t> importances(nbits);
285  Float_t importances_norm = 0;
286 
287  for (UInt_t i = 0; i < nbits; i++)importances[i] = 0;
288 
289  Float_t SROC, SSROC; //computed ROC value for every Seed and SubSeed
290 
291  x = range;
292 
293  for (UInt_t n = 0; n < seeds; n++) {
294  x = rangen -> Integer(range);
295 
296  std::bitset<NBITS> xbitset(x);
297  if (x == 0) continue; //dataloader need at least one variable
298 
299 
300  //creating loader for seed
301  TMVA::DataLoader *seeddl = new TMVA::DataLoader(xbitset.to_string());
302 
303  //adding variables from seed
304  for (UInt_t index = 0; index < nbits; index++) {
305  if (xbitset[index]) seeddl->AddVariable(varNames[index], 'F');
306  }
307 
308  //Loading Dataset
309  DataLoaderCopy(seeddl,fDataLoader.get());
310 
311  //Booking Seed
312  fClassifier->BookMethod(seeddl, methodName, methodTitle, methodOptions);
313 
314  //Train/Test/Evaluation
315  fClassifier->TrainAllMethods();
316  fClassifier->TestAllMethods();
317  fClassifier->EvaluateAllMethods();
318 
319  //getting ROC
320  SROC = fClassifier->GetROCIntegral(xbitset.to_string(), methodTitle);
321 
322  delete seeddl;
323  fClassifier->DeleteAllMethods();
324 
325  for (uint32_t i = 0; i < 32; ++i) {
326  if (x & (1 << i)) {
327  y = x & ~(1 << i);
328  std::bitset<NBITS> ybitset(y);
329  //need at least one variable
330  //NOTE: if subssed is zero then is the special case
331  //that count in xbitset is 1
332  Double_t ny = log(x - y) / 0.693147;
333  if (y == 0) {
334  importances[ny] = SROC - 0.5;
335  importances_norm += importances[ny];
336  continue;
337  }
338 
339  //creating loader for subseed
340  TMVA::DataLoader *subseeddl = new TMVA::DataLoader(ybitset.to_string());
341  //adding variables from subseed
342  for (UInt_t index = 0; index < nbits; index++) {
343  if (ybitset[index]) subseeddl->AddVariable(varNames[index], 'F');
344  }
345 
346  //Loading Dataset
347  DataLoaderCopy(subseeddl,fDataLoader.get());
348 
349  //Booking SubSeed
350  fClassifier->BookMethod(subseeddl, methodName, methodTitle, methodOptions);
351 
352  //Train/Test/Evaluation
353  fClassifier->TrainAllMethods();
354  fClassifier->TestAllMethods();
355  fClassifier->EvaluateAllMethods();
356 
357  //getting ROC
358  SSROC = fClassifier->GetROCIntegral(ybitset.to_string(), methodTitle);
359  importances[ny] += SROC - SSROC;
360 
361  delete subseeddl;
362  fClassifier->DeleteAllMethods();
363  }
364  }
365  }
366 
367  Float_t normalization = 0.0;
368  for (UInt_t i = 0; i < nbits; i++) normalization += importances[i];
369 
370  for(UInt_t i=0;i<nbits;i++){
371  //adding values
372  fResults.fImportanceValues[varNames[i]]=(100.0 * importances[i] / normalization);
373  //adding sufix
374  fResults.fImportanceValues[varNames[i]]=fResults.fImportanceValues.GetValue<TString>(varNames[i])+" % ";
375  }
376  fResults.fImportanceHist = std::shared_ptr<TH1F>(GetImportance(nbits,importances,varNames));
377  delete rangen;
378 
379 }
380 
381 
383 {
384 
385  TString methodName = fMethod.GetValue<TString>("MethodName");
386  TString methodTitle = fMethod.GetValue<TString>("MethodTitle");
387  TString methodOptions = fMethod.GetValue<TString>("MethodOptions");
388 
389  uint32_t x = 0;
390  uint32_t y = 0;
391 
392  //getting number of variables and variable names from loader
393  const UInt_t nbits = fDataLoader->GetDefaultDataSetInfo().GetNVariables();
394  std::vector<TString> varNames = fDataLoader->GetDefaultDataSetInfo().GetListOfVariables();
395 
396  ULong_t range = pow(2, nbits);
397 
398  //vector to save importances
399  std::vector<Float_t> importances(nbits);
400 
401  //vector to save ROC-Integral values
402  std::vector<Float_t> ROC(range);
403  ROC[0]=0.5;
404  for (UInt_t i = 0; i < nbits; i++) importances[i] = 0;
405 
406  Float_t SROC, SSROC; //computed ROC value
407  for ( x = 1; x <range ; x++) {
408 
409  std::bitset<NBITS> xbitset(x);
410  if (x == 0) continue; //dataloader need at least one variable
411 
412  //creating loader for seed
413  TMVA::DataLoader *seeddl = new TMVA::DataLoader(xbitset.to_string());
414 
415  //adding variables from seed
416  for (UInt_t index = 0; index < nbits; index++) {
417  if (xbitset[index]) seeddl->AddVariable(varNames[index], 'F');
418  }
419 
420  DataLoaderCopy(seeddl,fDataLoader.get());
421 
422  seeddl->PrepareTrainingAndTestTree(fDataLoader->GetDefaultDataSetInfo().GetCut("Signal"), fDataLoader->GetDefaultDataSetInfo().GetCut("Background"), fDataLoader->GetDefaultDataSetInfo().GetSplitOptions());
423 
424  //Booking Seed
425  fClassifier->BookMethod(seeddl, methodName, methodTitle, methodOptions);
426 
427  //Train/Test/Evaluation
428  fClassifier->TrainAllMethods();
429  fClassifier->TestAllMethods();
430  fClassifier->EvaluateAllMethods();
431 
432  //getting ROC
433  ROC[x] = fClassifier->GetROCIntegral(xbitset.to_string(), methodTitle);
434 
435  delete seeddl;
436  fClassifier->DeleteAllMethods();
437  }
438 
439 
440  for ( x = 0; x <range ; x++)
441  {
442  SROC=ROC[x];
443  for (uint32_t i = 0; i < NBITS; ++i) {
444  if (x & (1 << i)) {
445  y = x & ~(1 << i);
446  std::bitset<NBITS> ybitset(y);
447 
448  Float_t ny = log(x - y) / 0.693147;
449  if (y == 0) {
450  importances[ny] = SROC - 0.5;
451  continue;
452  }
453 
454  //getting ROC
455  SSROC = ROC[y];
456  importances[ny] += SROC - SSROC;
457  }
458 
459  }
460  }
461  Float_t normalization = 0.0;
462  for (UInt_t i = 0; i < nbits; i++) normalization += importances[i];
463 
464  for(UInt_t i=0;i<nbits;i++){
465  //adding values
466  fResults.fImportanceValues[varNames[i]]=(100.0 * importances[i] / normalization);
467  //adding sufix
468  fResults.fImportanceValues[varNames[i]]=fResults.fImportanceValues.GetValue<TString>(varNames[i])+" % ";
469  }
470  fResults.fImportanceHist = std::shared_ptr<TH1F>(GetImportance(nbits,importances,varNames));
471 }
472 
473 
474 
475 
Config & gConfig()
Definition: Config.cxx:43
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title Offset is a correction factor with respect to the "s...
Definition: TAttAxis.cxx:262
static long int sum(long int i)
Definition: Factory.cxx:1785
Random number generator class based on M.
Definition: TRandom3.h:29
MsgLogger & Endl(MsgLogger &ml)
Definition: MsgLogger.h:162
virtual void LabelsOption(Option_t *option="h", Option_t *axis="X")
Set option(s) to draw axis with labels.
Definition: TH1.cxx:4908
float Float_t
Definition: RtypesCore.h:53
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:8008
virtual void Evaluate()
Virtual method to be implmented with your algorithm.
return c
T GetValue(const TString &key)
Definition: OptionMap.h:152
VIType
Definition: Types.h:75
R__EXTERN TStyle * gStyle
Definition: TStyle.h:418
THist< 1, float, THistStatContent, THistStatUncertainty > TH1F
Definition: THist.hxx:302
Basic string class.
Definition: TString.h:137
tomato 1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:575
const Bool_t kFALSE
Definition: Rtypes.h:92
void DataLoaderCopy(TMVA::DataLoader *des, TMVA::DataLoader *src)
Definition: DataLoader.cxx:794
void CenterTitle(Bool_t center=kTRUE)
Center axis title.
Definition: TAxis.h:190
void AddVariable(const TString &expression, const TString &title, const TString &unit, char type='F', Double_t min=0, Double_t max=0)
Definition: DataLoader.cxx:455
std::shared_ptr< TH1F > fImportanceHist
virtual void SetBarWidth(Float_t width=0.5)
Definition: TH1.h:362
const char * Data() const
Definition: TString.h:349
virtual void SetRangeUser(Double_t ufirst, Double_t ulast)
Set the viewing range for the axis from ufirst to ulast (in user coordinates).
Definition: TAxis.cxx:925
Double_t x[n]
Definition: legend1.C:17
OptionMap fMethod
Definition: Envelope.h:58
const int ny
Definition: kalman.C:17
std::unique_ptr< Factory > fClassifier
double pow(double, double)
void Print() const
Definition: OptionMap.h:143
std::vector< std::vector< double > > Data
Base class for all machine learning algorithms.
Definition: Envelope.h:55
TCanvas * Draw(const TString name="VariableImportance") const
VariableImportance(DataLoader *loader)
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition: TAttFill.h:42
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:8280
unsigned int UInt_t
Definition: RtypesCore.h:42
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:1706
TAxis * GetYaxis()
Definition: TH1.h:325
virtual void SetTitleSize(Float_t size=0.04)
Set size of axis title The size is expressed in per cent of the pad width.
Definition: TAttAxis.cxx:272
The Canvas class.
Definition: TCanvas.h:41
#define NBITS
void PrepareTrainingAndTestTree(const TCut &cut, const TString &splitOpt)
Definition: DataLoader.cxx:579
double Double_t
Definition: RtypesCore.h:55
std::shared_ptr< DataLoader > fDataLoader
Definition: Envelope.h:59
unsigned long ULong_t
Definition: RtypesCore.h:51
Double_t y[n]
Definition: legend1.C:17
MsgLogger & Log() const
Definition: Configurable.h:128
virtual void SetBinLabel(Int_t bin, const char *label)
Set label for bin.
Definition: TAxis.cxx:808
TH1F * GetImportance(const UInt_t nbits, std::vector< Float_t > &importances, std::vector< TString > &varNames)
virtual void Draw(Option_t *option="")
Draw a canvas.
Definition: TCanvas.cxx:795
void SetTitleXOffset(Float_t offset=1)
Definition: TStyle.h:398
Abstract ClassifierFactory template that handles arbitrary types.
#define nullptr
Definition: Rtypes.h:87
void SetSilent(Bool_t s)
Definition: Config.h:64
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:1257
static void EnableOutput()
Definition: MsgLogger.cxx:70
VariableImportanceResult fResults
const Bool_t kTRUE
Definition: Rtypes.h:91
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition: TNamed.cxx:155
void EvaluateImportanceRandom(UInt_t nseeds)
const Int_t n
Definition: legend1.C:16
char name[80]
Definition: TGX11.cxx:109
double log(double)
TAxis * GetXaxis()
Definition: TH1.h:324