| 1 | /* ======================================================================== *\
|
|---|
| 2 | ! $Name: not supported by cvs2svn $:$Id: MHRanForestGini.cc,v 1.9 2008-11-11 11:46:50 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 <TH1.h>
|
|---|
| 39 | #include <TPad.h>
|
|---|
| 40 | #include <TGraph.h>
|
|---|
| 41 | #include <TStyle.h>
|
|---|
| 42 | #include <TCanvas.h>
|
|---|
| 43 | #include <TMarker.h>
|
|---|
| 44 |
|
|---|
| 45 | #include "MParList.h"
|
|---|
| 46 | #include "MBinning.h"
|
|---|
| 47 | #include "MRanTree.h"
|
|---|
| 48 | #include "MRanForest.h"
|
|---|
| 49 | #include "MDataArray.h"
|
|---|
| 50 |
|
|---|
| 51 | #include "MLog.h"
|
|---|
| 52 | #include "MLogManip.h"
|
|---|
| 53 |
|
|---|
| 54 | ClassImp(MHRanForestGini);
|
|---|
| 55 |
|
|---|
| 56 | using namespace std;
|
|---|
| 57 |
|
|---|
| 58 | // --------------------------------------------------------------------------
|
|---|
| 59 | //
|
|---|
| 60 | // Setup histograms, nbins is the number of bins used for the evaluation.
|
|---|
| 61 | // The default is 100 bins.
|
|---|
| 62 | //
|
|---|
| 63 | MHRanForestGini::MHRanForestGini(Int_t nbins, const char *name, const char *title)
|
|---|
| 64 | : fRules(0.01, 0.01, 0.99, 0.99)
|
|---|
| 65 | {
|
|---|
| 66 | //
|
|---|
| 67 | // set the name and title of this object
|
|---|
| 68 | //
|
|---|
| 69 | fName = name ? name : "MHRanForestGini";
|
|---|
| 70 | fTitle = title ? title : "Measure of importance of Random Forest-input parameters";
|
|---|
| 71 |
|
|---|
| 72 | fGraphGini.SetNameTitle("Gini", "Importance of RF-input parameters measured by mean Gini decrease");
|
|---|
| 73 | fGraphGini.SetMarkerStyle(kFullDotMedium);
|
|---|
| 74 |
|
|---|
| 75 | fGraphError.SetNameTitle("ResErr", "Resolution/Error versus train step");
|
|---|
| 76 | fGraphError.SetMarkerStyle(kFullDotMedium);
|
|---|
| 77 |
|
|---|
| 78 | fGraphNodes.SetNameTitle("Nodes", "Number of nodes versus train step");
|
|---|
| 79 | fGraphNodes.SetMarkerStyle(kFullDotMedium);
|
|---|
| 80 |
|
|---|
| 81 | fRules.SetTextAlign(13);
|
|---|
| 82 | fRules.SetTextSize(0.05);
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | // --------------------------------------------------------------------------
|
|---|
| 86 | //
|
|---|
| 87 | // Setup Filling of the histograms. It needs:
|
|---|
| 88 | // MMcEvt and MRanForest
|
|---|
| 89 | //
|
|---|
| 90 | Bool_t MHRanForestGini::SetupFill(const MParList *plist)
|
|---|
| 91 | {
|
|---|
| 92 | fRanForest = (MRanForest*)plist->FindObject("MRanForest");
|
|---|
| 93 | if (!fRanForest)
|
|---|
| 94 | {
|
|---|
| 95 | *fLog << err << dbginf << "MRanForest not found... aborting." << endl;
|
|---|
| 96 | return kERROR;
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | fGini.Set(fRanForest->GetNumDim());
|
|---|
| 100 | return kTRUE;
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | // --------------------------------------------------------------------------
|
|---|
| 104 | //
|
|---|
| 105 | // Fill the RanForest from a MRanForest container into the corresponding
|
|---|
| 106 | // histogram dependant on the particle id.
|
|---|
| 107 | //
|
|---|
| 108 | //
|
|---|
| 109 | Int_t MHRanForestGini::Fill(const MParContainer *par, const Stat_t w)
|
|---|
| 110 | {
|
|---|
| 111 | MRanTree *t = fRanForest->GetCurTree();
|
|---|
| 112 |
|
|---|
| 113 | for (Int_t i=0;i<fRanForest->GetNumDim();i++)
|
|---|
| 114 | fGini[i] += t->GetGiniDec(i);
|
|---|
| 115 |
|
|---|
| 116 | fGraphError.SetPoint(fGraphError.GetN(), GetNumExecutions(), t->GetError());
|
|---|
| 117 | fGraphNodes.SetPoint(fGraphError.GetN(), GetNumExecutions(), t->GetNumEndNodes());
|
|---|
| 118 |
|
|---|
| 119 | return kTRUE;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | // --------------------------------------------------------------------------
|
|---|
| 123 | //
|
|---|
| 124 | //
|
|---|
| 125 | Bool_t MHRanForestGini::Finalize()
|
|---|
| 126 | {
|
|---|
| 127 | // --- Calculate mean decrease of gini index ---
|
|---|
| 128 | const Int_t n = fGini.GetSize();
|
|---|
| 129 |
|
|---|
| 130 | fGraphGini.Set(n);
|
|---|
| 131 |
|
|---|
| 132 | for (Int_t i=0; i<n; i++)
|
|---|
| 133 | {
|
|---|
| 134 | fGini[i] /= fRanForest->GetNumTrees()*fRanForest->GetNumData();
|
|---|
| 135 | fGraphGini.SetPoint(i, i+1, fGini[i]);
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | // --- Produce some text information ---
|
|---|
| 139 | fRules.AddText("");
|
|---|
| 140 | fRules.AddText(Form("%s w/ %d trees of node size %d trained by %d evts",
|
|---|
| 141 | fRanForest->IsClassify()?"Classification":"Regression",
|
|---|
| 142 | fRanForest->GetNumTrees(),
|
|---|
| 143 | fRanForest->GetNdSize(),
|
|---|
| 144 | fRanForest->GetNumData()));
|
|---|
| 145 | fRules.AddText("---");//Form("---> %s", fRanForest->GetTargetRule().Data()));
|
|---|
| 146 |
|
|---|
| 147 | const MDataArray &arr = *fRanForest->GetRules();
|
|---|
| 148 |
|
|---|
| 149 | int i;
|
|---|
| 150 | for (i=0; i<arr.GetNumEntries(); i++)
|
|---|
| 151 | fRules.AddText(Form("%d) %s", i+1, arr.GetRule(i).Data()));
|
|---|
| 152 |
|
|---|
| 153 | for (; i<20; i++)
|
|---|
| 154 | fRules.AddText("");
|
|---|
| 155 |
|
|---|
| 156 | return kTRUE;
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | // --------------------------------------------------------------------------
|
|---|
| 160 | //
|
|---|
| 161 | // Draw histogram. (For the Meaning see class description)
|
|---|
| 162 | //
|
|---|
| 163 | void MHRanForestGini::Draw(Option_t *)
|
|---|
| 164 | {
|
|---|
| 165 | if (fGraphGini.GetN()==0)
|
|---|
| 166 | return;
|
|---|
| 167 |
|
|---|
| 168 | TVirtualPad *pad = gPad ? gPad : MakeDefCanvas(this);
|
|---|
| 169 | pad->SetBorderMode(0);
|
|---|
| 170 |
|
|---|
| 171 | AppendPad("");
|
|---|
| 172 |
|
|---|
| 173 | pad->Divide(2,2);
|
|---|
| 174 |
|
|---|
| 175 | pad->cd(1);
|
|---|
| 176 | gPad->SetBorderMode(0);
|
|---|
| 177 | gPad->SetGridx();
|
|---|
| 178 | gPad->SetGridy();
|
|---|
| 179 | fGraphGini.Draw("ALP");
|
|---|
| 180 |
|
|---|
| 181 | TH1 *h = fGraphGini.GetHistogram();
|
|---|
| 182 | if (h)
|
|---|
| 183 | {
|
|---|
| 184 | h->SetXTitle("No.of RF-input parameter");
|
|---|
| 185 | h->SetYTitle("Mean decrease in Gini-index [au]");
|
|---|
| 186 | h->GetXaxis()->SetNdivisions(10);
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | pad->cd(2);
|
|---|
| 190 | gPad->SetBorderMode(0);
|
|---|
| 191 | fGraphError.Draw("ALP");
|
|---|
| 192 | h = fGraphError.GetHistogram();
|
|---|
| 193 | if (h)
|
|---|
| 194 | {
|
|---|
| 195 | h->SetXTitle("Train step/Tree number");
|
|---|
| 196 | h->SetYTitle("Error/Resolution");
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | pad->cd(3);
|
|---|
| 200 | gPad->SetBorderMode(0);
|
|---|
| 201 | fGraphNodes.Draw("ALP");
|
|---|
| 202 | h = fGraphNodes.GetHistogram();
|
|---|
| 203 | if (h)
|
|---|
| 204 | {
|
|---|
| 205 | h->SetXTitle("Train step/Tree number");
|
|---|
| 206 | h->SetYTitle("Number of end nodes");
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | pad->cd(4);
|
|---|
| 210 | gPad->SetBorderMode(0);
|
|---|
| 211 | fRules.Draw();
|
|---|
| 212 | }
|
|---|