| 1 | /* ======================================================================== *\
|
|---|
| 2 | ! $Name: not supported by cvs2svn $:$Id: MHRanForestGini.cc,v 1.7 2006-11-02 08:57:00 tbretz Exp $
|
|---|
| 3 | ! --------------------------------------------------------------------------
|
|---|
| 4 | !
|
|---|
| 5 | ! *
|
|---|
| 6 | ! * This file is part of MARS, the MAGIC Analysis and Reconstruction
|
|---|
| 7 | ! * Software. It is distributed to you in the hope that it can be a useful
|
|---|
| 8 | ! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
|
|---|
| 9 | ! * It is distributed WITHOUT ANY WARRANTY.
|
|---|
| 10 | ! *
|
|---|
| 11 | ! * Permission to use, copy, modify and distribute this software and its
|
|---|
| 12 | ! * documentation for any purpose is hereby granted without fee,
|
|---|
| 13 | ! * provided that the above copyright notice appear in all copies and
|
|---|
| 14 | ! * that both that copyright notice and this permission notice appear
|
|---|
| 15 | ! * in supporting documentation. It is provided "as is" without express
|
|---|
| 16 | ! * or implied warranty.
|
|---|
| 17 | ! *
|
|---|
| 18 | !
|
|---|
| 19 | !
|
|---|
| 20 | ! Author(s): Thomas Hengstebeck 3/2003 <mailto:hengsteb@alwa02.physik.uni-siegen.de>
|
|---|
| 21 | ! Author(s): Thomas Bretz <mailto:tbretz@astro.uni-wuerzburg.de>
|
|---|
| 22 | !
|
|---|
| 23 | ! Copyright: MAGIC Software Development, 2000-2006
|
|---|
| 24 | !
|
|---|
| 25 | !
|
|---|
| 26 | \* ======================================================================== */
|
|---|
| 27 |
|
|---|
| 28 | /////////////////////////////////////////////////////////////////////////////
|
|---|
| 29 | //
|
|---|
| 30 | // MHRanForest
|
|---|
| 31 | //
|
|---|
| 32 | // This histogram shows a measure of variable importance (mean decrease in
|
|---|
| 33 | // Gini-index)
|
|---|
| 34 | //
|
|---|
| 35 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 36 | #include "MHRanForestGini.h"
|
|---|
| 37 |
|
|---|
| 38 | #include <TPad.h>
|
|---|
| 39 | #include <TGraph.h>
|
|---|
| 40 | #include <TStyle.h>
|
|---|
| 41 | #include <TCanvas.h>
|
|---|
| 42 | #include <TMarker.h>
|
|---|
| 43 |
|
|---|
| 44 | #include "MParList.h"
|
|---|
| 45 | #include "MBinning.h"
|
|---|
| 46 | #include "MRanTree.h"
|
|---|
| 47 | #include "MRanForest.h"
|
|---|
| 48 | #include "MDataArray.h"
|
|---|
| 49 |
|
|---|
| 50 | #include "MLog.h"
|
|---|
| 51 | #include "MLogManip.h"
|
|---|
| 52 |
|
|---|
| 53 | ClassImp(MHRanForestGini);
|
|---|
| 54 |
|
|---|
| 55 | using namespace std;
|
|---|
| 56 |
|
|---|
| 57 | // --------------------------------------------------------------------------
|
|---|
| 58 | //
|
|---|
| 59 | // Setup histograms, nbins is the number of bins used for the evaluation.
|
|---|
| 60 | // The default is 100 bins.
|
|---|
| 61 | //
|
|---|
| 62 | MHRanForestGini::MHRanForestGini(Int_t nbins, const char *name, const char *title)
|
|---|
| 63 | : fRules(0.01, 0.01, 0.99, 0.99)
|
|---|
| 64 | {
|
|---|
| 65 | //
|
|---|
| 66 | // set the name and title of this object
|
|---|
| 67 | //
|
|---|
| 68 | fName = name ? name : "MHRanForestGini";
|
|---|
| 69 | fTitle = title ? title : "Measure of importance of Random Forest-input parameters";
|
|---|
| 70 |
|
|---|
| 71 | fGraphGini.SetNameTitle("Gini", "Importance of RF-input parameters measured by mean Gini decrease");
|
|---|
| 72 | fGraphGini.SetMarkerStyle(kFullDotMedium);
|
|---|
| 73 |
|
|---|
| 74 | fGraphError.SetNameTitle("ResErr", "Resolution/Error versus train step");
|
|---|
| 75 | fGraphError.SetMarkerStyle(kFullDotMedium);
|
|---|
| 76 |
|
|---|
| 77 | fGraphNodes.SetNameTitle("Nodes", "Number of nodes versus train step");
|
|---|
| 78 | fGraphNodes.SetMarkerStyle(kFullDotMedium);
|
|---|
| 79 |
|
|---|
| 80 | fRules.SetTextAlign(13);
|
|---|
| 81 | fRules.SetTextSize(0.05);
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | // --------------------------------------------------------------------------
|
|---|
| 85 | //
|
|---|
| 86 | // Setup Filling of the histograms. It needs:
|
|---|
| 87 | // MMcEvt and MRanForest
|
|---|
| 88 | //
|
|---|
| 89 | Bool_t MHRanForestGini::SetupFill(const MParList *plist)
|
|---|
| 90 | {
|
|---|
| 91 | fRanForest = (MRanForest*)plist->FindObject("MRanForest");
|
|---|
| 92 | if (!fRanForest)
|
|---|
| 93 | {
|
|---|
| 94 | *fLog << err << dbginf << "MRanForest not found... aborting." << endl;
|
|---|
| 95 | return kFALSE;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | fGini.Set(fRanForest->GetNumDim());
|
|---|
| 99 | return kTRUE;
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | // --------------------------------------------------------------------------
|
|---|
| 103 | //
|
|---|
| 104 | // Fill the RanForest from a MRanForest container into the corresponding
|
|---|
| 105 | // histogram dependant on the particle id.
|
|---|
| 106 | //
|
|---|
| 107 | //
|
|---|
| 108 | Bool_t MHRanForestGini::Fill(const MParContainer *par, const Stat_t w)
|
|---|
| 109 | {
|
|---|
| 110 | MRanTree *t = fRanForest->GetCurTree();
|
|---|
| 111 |
|
|---|
| 112 | for (Int_t i=0;i<fRanForest->GetNumDim();i++)
|
|---|
| 113 | fGini[i] += t->GetGiniDec(i);
|
|---|
| 114 |
|
|---|
| 115 | fGraphError.SetPoint(fGraphError.GetN(), GetNumExecutions(), t->GetError());
|
|---|
| 116 | fGraphNodes.SetPoint(fGraphError.GetN(), GetNumExecutions(), t->GetNumEndNodes());
|
|---|
| 117 |
|
|---|
| 118 | return kTRUE;
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | // --------------------------------------------------------------------------
|
|---|
| 122 | //
|
|---|
| 123 | //
|
|---|
| 124 | Bool_t MHRanForestGini::Finalize()
|
|---|
| 125 | {
|
|---|
| 126 | // --- Calculate mean decrease of gini index ---
|
|---|
| 127 | const Int_t n = fGini.GetSize();
|
|---|
| 128 |
|
|---|
| 129 | fGraphGini.Set(n);
|
|---|
| 130 |
|
|---|
| 131 | for (Int_t i=0; i<n; i++)
|
|---|
| 132 | {
|
|---|
| 133 | fGini[i] /= fRanForest->GetNumTrees()*fRanForest->GetNumData();
|
|---|
| 134 | fGraphGini.SetPoint(i, i+1, fGini[i]);
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | // --- Produce some text information ---
|
|---|
| 138 | fRules.AddText("");
|
|---|
| 139 | fRules.AddText(Form("%s w/ %d trees of node size %d trained by %d evts",
|
|---|
| 140 | fRanForest->IsClassify()?"Classification":"Regression",
|
|---|
| 141 | fRanForest->GetNumTrees(),
|
|---|
| 142 | fRanForest->GetNdSize(),
|
|---|
| 143 | fRanForest->GetNumData()));
|
|---|
| 144 | fRules.AddText("---");//Form("---> %s", fRanForest->GetTargetRule().Data()));
|
|---|
| 145 |
|
|---|
| 146 | const MDataArray &arr = *fRanForest->GetRules();
|
|---|
| 147 |
|
|---|
| 148 | int i;
|
|---|
| 149 | for (i=0; i<arr.GetNumEntries(); i++)
|
|---|
| 150 | fRules.AddText(Form("%d) %s", i+1, arr.GetRule(i).Data()));
|
|---|
| 151 |
|
|---|
| 152 | for (; i<20; i++)
|
|---|
| 153 | fRules.AddText("");
|
|---|
| 154 |
|
|---|
| 155 | return kTRUE;
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | // --------------------------------------------------------------------------
|
|---|
| 159 | //
|
|---|
| 160 | // Draw histogram. (For the Meaning see class description)
|
|---|
| 161 | //
|
|---|
| 162 | void MHRanForestGini::Draw(Option_t *)
|
|---|
| 163 | {
|
|---|
| 164 | if (fGraphGini.GetN()==0)
|
|---|
| 165 | return;
|
|---|
| 166 |
|
|---|
| 167 | TVirtualPad *pad = gPad ? gPad : MakeDefCanvas(this);
|
|---|
| 168 | pad->SetBorderMode(0);
|
|---|
| 169 |
|
|---|
| 170 | AppendPad("");
|
|---|
| 171 |
|
|---|
| 172 | pad->Divide(2,2);
|
|---|
| 173 |
|
|---|
| 174 | pad->cd(1);
|
|---|
| 175 | gPad->SetBorderMode(0);
|
|---|
| 176 | gPad->SetGridx();
|
|---|
| 177 | gPad->SetGridy();
|
|---|
| 178 | fGraphGini.Draw("ALP");
|
|---|
| 179 |
|
|---|
| 180 | TH1 *h = fGraphGini.GetHistogram();
|
|---|
| 181 | if (h)
|
|---|
| 182 | {
|
|---|
| 183 | h->SetXTitle("No.of RF-input parameter");
|
|---|
| 184 | h->SetYTitle("Mean decrease in Gini-index [au]");
|
|---|
| 185 | h->GetXaxis()->SetNdivisions(10);
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | pad->cd(2);
|
|---|
| 189 | gPad->SetBorderMode(0);
|
|---|
| 190 | fGraphError.Draw("ALP");
|
|---|
| 191 | h = fGraphError.GetHistogram();
|
|---|
| 192 | if (h)
|
|---|
| 193 | {
|
|---|
| 194 | h->SetXTitle("Train step/Tree number");
|
|---|
| 195 | h->SetYTitle("Error/Resolution");
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | pad->cd(3);
|
|---|
| 199 | gPad->SetBorderMode(0);
|
|---|
| 200 | fGraphNodes.Draw("ALP");
|
|---|
| 201 | h = fGraphNodes.GetHistogram();
|
|---|
| 202 | if (h)
|
|---|
| 203 | {
|
|---|
| 204 | h->SetXTitle("Train step/Tree number");
|
|---|
| 205 | h->SetYTitle("Number of end nodes");
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 | pad->cd(4);
|
|---|
| 209 | gPad->SetBorderMode(0);
|
|---|
| 210 | fRules.Draw();
|
|---|
| 211 | }
|
|---|