source: trunk/Mars/mranforest/MHRanForestGini.cc@ 9892

Last change on this file since 9892 was 9865, checked in by tbretz, 14 years ago
Improved display of MHRanForestGini.
File size: 6.2 KB
Line 
1/* ======================================================================== *\
2! $Name: not supported by cvs2svn $:$Id: MHRanForestGini.cc,v 1.10 2010-06-03 11:20:47 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
54ClassImp(MHRanForestGini);
55
56using 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//
63MHRanForestGini::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(28);
74 fGraphGini.SetFillColor(38);
75
76 fGraphError.SetNameTitle("ResErr", "Resolution/Error versus train step");
77 fGraphError.SetMarkerStyle(kFullDotMedium);
78
79 fGraphNodes.SetNameTitle("Nodes", "Number of nodes versus train step");
80 fGraphNodes.SetMarkerStyle(kFullDotMedium);
81
82 fRules.SetTextAlign(13);
83 fRules.SetTextSize(0.05);
84}
85
86// --------------------------------------------------------------------------
87//
88// Setup Filling of the histograms. It needs:
89// MMcEvt and MRanForest
90//
91Bool_t MHRanForestGini::SetupFill(const MParList *plist)
92{
93 fRanForest = (MRanForest*)plist->FindObject("MRanForest");
94 if (!fRanForest)
95 {
96 *fLog << err << dbginf << "MRanForest not found... aborting." << endl;
97 return kERROR;
98 }
99
100 fGini.Set(fRanForest->GetNumDim());
101 return kTRUE;
102}
103
104// --------------------------------------------------------------------------
105//
106// Fill the RanForest from a MRanForest container into the corresponding
107// histogram dependant on the particle id.
108//
109//
110Int_t MHRanForestGini::Fill(const MParContainer *par, const Stat_t w)
111{
112 MRanTree *t = fRanForest->GetCurTree();
113
114 for (Int_t i=0;i<fRanForest->GetNumDim();i++)
115 fGini[i] += t->GetGiniDec(i);
116
117 fGraphError.SetPoint(fGraphError.GetN(), GetNumExecutions(), t->GetError());
118 fGraphNodes.SetPoint(fGraphError.GetN(), GetNumExecutions(), t->GetNumEndNodes());
119
120 return kTRUE;
121}
122
123// --------------------------------------------------------------------------
124//
125//
126Bool_t MHRanForestGini::Finalize()
127{
128 // --- Calculate mean decrease of gini index ---
129 const Int_t n = fGini.GetSize();
130
131 fGraphGini.Set(n);
132
133 for (Int_t i=0; i<n; i++)
134 {
135 fGini[i] /= fRanForest->GetNumTrees()*fRanForest->GetNumData();
136 fGraphGini.SetPoint(i, i+1, fGini[i]);
137 }
138
139 // --- Produce some text information ---
140 fRules.AddText("");
141 fRules.AddText(Form("%s w/ %d trees of node size %d trained by %d evts",
142 fRanForest->IsClassify()?"Classification":"Regression",
143 fRanForest->GetNumTrees(),
144 fRanForest->GetNdSize(),
145 fRanForest->GetNumData()));
146 fRules.AddText("---");//Form("---> %s", fRanForest->GetTargetRule().Data()));
147
148 const MDataArray &arr = *fRanForest->GetRules();
149
150 int i;
151 for (i=0; i<arr.GetNumEntries(); i++)
152 fRules.AddText(Form("%d) %s", i+1, arr.GetRule(i).Data()));
153
154 for (; i<20; i++)
155 fRules.AddText("");
156
157 return kTRUE;
158}
159
160// --------------------------------------------------------------------------
161//
162// Draw histogram. (For the Meaning see class description)
163//
164void MHRanForestGini::Draw(Option_t *)
165{
166 if (fGraphGini.GetN()==0)
167 return;
168
169 TVirtualPad *pad = gPad ? gPad : MakeDefCanvas(this);
170 pad->SetBorderMode(0);
171
172 AppendPad("");
173
174 pad->Divide(2,2);
175
176 pad->cd(1);
177 gPad->SetBorderMode(0);
178 gPad->SetGrid();
179 fGraphGini.Draw("AB");
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 h->SetMinimum(0);
188 }
189
190 pad->cd(2);
191 gPad->SetBorderMode(0);
192 gPad->SetGridy();
193 fGraphError.Draw("ALP");
194 h = fGraphError.GetHistogram();
195 if (h)
196 {
197 h->SetXTitle("Train step/Tree number");
198 h->SetYTitle("Error/Resolution");
199 h->SetMinimum(0);
200 }
201
202 pad->cd(3);
203 gPad->SetBorderMode(0);
204 gPad->SetGridy();
205 fGraphNodes.Draw("ALP");
206 h = fGraphNodes.GetHistogram();
207 if (h)
208 {
209 h->SetXTitle("Train step/Tree number");
210 h->SetYTitle("Number of end nodes");
211 h->SetMinimum(0);
212 }
213
214 pad->cd(4);
215 gPad->SetBorderMode(0);
216 fRules.Draw();
217}
Note: See TracBrowser for help on using the repository browser.