source: releases/Mars.2014.05.26/mranforest/MHRanForestGini.cc@ 19089

Last change on this file since 19089 was 10166, checked in by tbretz, 14 years ago
Removed the old obsolete cvs header line.
File size: 6.0 KB
Line 
1/* ======================================================================== *\
2!
3! *
4! * This file is part of MARS, the MAGIC Analysis and Reconstruction
5! * Software. It is distributed to you in the hope that it can be a useful
6! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
7! * It is distributed WITHOUT ANY WARRANTY.
8! *
9! * Permission to use, copy, modify and distribute this software and its
10! * documentation for any purpose is hereby granted without fee,
11! * provided that the above copyright notice appear in all copies and
12! * that both that copyright notice and this permission notice appear
13! * in supporting documentation. It is provided "as is" without express
14! * or implied warranty.
15! *
16!
17!
18! Author(s): Thomas Hengstebeck 3/2003 <mailto:hengsteb@alwa02.physik.uni-siegen.de>
19! Author(s): Thomas Bretz <mailto:tbretz@astro.uni-wuerzburg.de>
20!
21! Copyright: MAGIC Software Development, 2000-2006
22!
23!
24\* ======================================================================== */
25
26/////////////////////////////////////////////////////////////////////////////
27//
28// MHRanForest
29//
30// This histogram shows a measure of variable importance (mean decrease in
31// Gini-index)
32//
33////////////////////////////////////////////////////////////////////////////
34#include "MHRanForestGini.h"
35
36#include <TH1.h>
37#include <TPad.h>
38#include <TGraph.h>
39#include <TStyle.h>
40#include <TCanvas.h>
41#include <TMarker.h>
42
43#include "MParList.h"
44#include "MBinning.h"
45#include "MRanTree.h"
46#include "MRanForest.h"
47#include "MDataArray.h"
48
49#include "MLog.h"
50#include "MLogManip.h"
51
52ClassImp(MHRanForestGini);
53
54using namespace std;
55
56// --------------------------------------------------------------------------
57//
58// Setup histograms, nbins is the number of bins used for the evaluation.
59// The default is 100 bins.
60//
61MHRanForestGini::MHRanForestGini(Int_t nbins, const char *name, const char *title)
62 : fRules(0.01, 0.01, 0.99, 0.99)
63{
64 //
65 // set the name and title of this object
66 //
67 fName = name ? name : "MHRanForestGini";
68 fTitle = title ? title : "Measure of importance of Random Forest-input parameters";
69
70 fGraphGini.SetNameTitle("Gini", "Importance of RF-input parameters measured by mean Gini decrease");
71 fGraphGini.SetMarkerStyle(28);
72 fGraphGini.SetFillColor(38);
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//
89Bool_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 kERROR;
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//
108Int_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//
124Bool_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//
162void 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->SetGrid();
177 fGraphGini.Draw("AB");
178
179 TH1 *h = fGraphGini.GetHistogram();
180 if (h)
181 {
182 h->SetXTitle("No.of RF-input parameter");
183 h->SetYTitle("Mean decrease in Gini-index [au]");
184 h->GetXaxis()->SetNdivisions(10);
185 h->SetMinimum(0);
186 }
187
188 pad->cd(2);
189 gPad->SetBorderMode(0);
190 gPad->SetGridy();
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 h->SetMinimum(0);
198 }
199
200 pad->cd(3);
201 gPad->SetBorderMode(0);
202 gPad->SetGridy();
203 fGraphNodes.Draw("ALP");
204 h = fGraphNodes.GetHistogram();
205 if (h)
206 {
207 h->SetXTitle("Train step/Tree number");
208 h->SetYTitle("Number of end nodes");
209 h->SetMinimum(0);
210 }
211
212 pad->cd(4);
213 gPad->SetBorderMode(0);
214 fRules.Draw();
215}
Note: See TracBrowser for help on using the repository browser.