#include <iostream>
#include <algorithm>
#include "TMVA/RuleFit.h"
#include "TMVA/MethodRuleFit.h"
ClassImp(TMVA::RuleFit)
;
TMVA::RuleFit::RuleFit( const TMVA::MethodRuleFit *rfbase,
const std::vector< TMVA::DecisionTree *> & forest,
const std::vector<TMVA::Event *> & trainingEvents,
Double_t samplefrac )
: fLogger( "RuleFit" )
{
Initialise( rfbase, forest, trainingEvents, samplefrac );
}
TMVA::RuleFit::RuleFit()
: fLogger( "RuleFit" )
{
}
TMVA::RuleFit::~RuleFit()
{
}
void TMVA::RuleFit::Initialise( const TMVA::MethodRuleFit *rfbase,
const std::vector< TMVA::DecisionTree *> & forest,
const std::vector< TMVA::Event *> & events,
Double_t sampfrac )
{
fMethodRuleFit = rfbase;
std::vector< TMVA::DecisionTree *>::const_iterator itrDtree=forest.begin();
for (; itrDtree!=forest.end(); ++itrDtree ) fForest.push_back( *itrDtree );
ForestStatistics();
SetTrainingEvents( events, sampfrac );
fRuleEnsemble.Initialize( this );
fLogger << kINFO << "make model" << Endl;
fRuleEnsemble.MakeModel();
fRuleFitParams.SetRuleFit( this );
}
void TMVA::RuleFit::Copy( const TMVA::RuleFit& other )
{
if(this != &other) {
fMethodRuleFit = other.GetMethodRuleFit();
fTrainingEvents = other.GetTrainingEvents();
fSubsampleEvents = other.GetSubsampleEvents();
fForest = other.GetForest();
fRuleEnsemble = other.GetRuleEnsemble();
}
}
void TMVA::RuleFit::ForestStatistics()
{
UInt_t ntrees = fForest.size();
Double_t nt = Double_t(ntrees);
const TMVA::DecisionTree *tree;
Double_t sumn2 = 0;
Double_t sumn = 0;
Double_t nd;
for (UInt_t i=0; i<ntrees; i++) {
tree = fForest[i];
nd = Double_t(tree->GetNNodes());
sumn += nd;
sumn2 += nd*nd;
}
Double_t var = (sumn2 - (sumn*sumn/nt))/(nt-1);
fLogger << kINFO << "nodes in trees, average& variance = " << sumn/nt << " , " << var << Endl;
}
void TMVA::RuleFit::FitCoefficients()
{
fRuleFitParams.MakeGDPath();
}
void TMVA::RuleFit::CalcImportance()
{
fLogger << kINFO << "calculating importance" << Endl;
fRuleEnsemble.CalcImportance();
fRuleEnsemble.CleanupRules();
fRuleEnsemble.CleanupLinear();
fRuleEnsemble.CalcVarImportance();
fLogger << kINFO << "filling rule statistics" << Endl;
fRuleEnsemble.RuleStatistics();
}
Double_t TMVA::RuleFit::EvalEvent( const TMVA::Event& e )
{
return fRuleEnsemble.EvalEvent( e );
}
void TMVA::RuleFit::SetTrainingEvents( const std::vector<TMVA::Event *>& el, Double_t sampfrac )
{
UInt_t neve = el.size();
if (neve==0) fLogger << kWARNING << "an empty sample of training events was given" << Endl;
fTrainingEvents.clear();
for (UInt_t i=0; i<el.size(); i++) {
fTrainingEvents.push_back(static_cast< const TMVA::Event *>(el[i]));
}
std::random_shuffle( fTrainingEvents.begin(), fTrainingEvents.end() );
Int_t istep = static_cast<Int_t>(neve*sampfrac);
Int_t nsub = static_cast<Int_t>(1.0/sampfrac);
for (Int_t s=0; s<nsub; s++) {
fSubsampleEvents.push_back( s*istep );
}
fSubsampleEvents.push_back( neve );
fLogger << kINFO << "created " << nsub << " training samples of in total "
<< fTrainingEvents.size() << " events" << Endl;
}
void TMVA::RuleFit::GetSubsampleEvents(Int_t sub, UInt_t& ibeg, UInt_t& iend) const
{
Int_t nsub = GetNSubsamples();
if (nsub==0) {
fLogger << kFATAL << "<GetSubsampleEvents> - wrong size, not properly initialised! BUG!!!" << Endl;
}
ibeg = 0;
iend = 0;
if (sub<0) {
ibeg = 0;
iend = fTrainingEvents.size() - 1;
}
if (sub<nsub) {
ibeg = fSubsampleEvents[sub];
iend = fSubsampleEvents[sub+1];
}
}
ROOT page - Class index - Class Hierarchy - Top of the page
This page has been automatically generated. If you have any comments or suggestions about the page layout send a mail to ROOT support, or contact the developers with any questions or problems regarding ROOT.