Logo ROOT  
Reference Guide
MethodKNN.h
Go to the documentation of this file.
1 // @(#)root/tmva $Id$
2 // Author: Rustem Ospanov
3 
4 /**********************************************************************************
5  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
6  * Package: TMVA *
7  * Class : MethodKNN *
8  * Web : http://tmva.sourceforge.net *
9  * *
10  * Description: *
11  * Analysis of k-nearest neighbor *
12  * *
13  * Author: *
14  * Rustem Ospanov <rustem@fnal.gov> - U. of Texas at Austin, USA *
15  * *
16  * Copyright (c) 2007: *
17  * CERN, Switzerland *
18  * MPI-K Heidelberg, Germany *
19  * U. of Texas at Austin, USA *
20  * *
21  * Redistribution and use in source and binary forms, with or without *
22  * modification, are permitted according to the terms listed in LICENSE *
23  * (http://tmva.sourceforge.net/LICENSE) *
24  **********************************************************************************/
25 
26 #ifndef ROOT_TMVA_MethodKNN
27 #define ROOT_TMVA_MethodKNN
28 
29 //////////////////////////////////////////////////////////////////////////
30 // //
31 // MethodKNN //
32 // //
33 // Analysis of k-nearest neighbor //
34 // //
35 //////////////////////////////////////////////////////////////////////////
36 
37 #include <vector>
38 
39 // Local
40 #include "TMVA/MethodBase.h"
41 #include "TMVA/ModulekNN.h"
42 
43 // SVD and linear discriminant code
44 #include "TMVA/LDA.h"
45 
46 namespace TMVA
47 {
48  namespace kNN
49  {
50  class ModulekNN;
51  }
52 
53  class MethodKNN : public MethodBase
54  {
55  public:
56 
57  MethodKNN(const TString& jobName,
58  const TString& methodTitle,
59  DataSetInfo& theData,
60  const TString& theOption = "KNN");
61 
62  MethodKNN(DataSetInfo& theData,
63  const TString& theWeightFile);
64 
65  virtual ~MethodKNN( void );
66 
67  virtual Bool_t HasAnalysisType( Types::EAnalysisType type, UInt_t numberClasses, UInt_t numberTargets );
68 
69  void Train( void );
70 
71  Double_t GetMvaValue( Double_t* err = 0, Double_t* errUpper = 0 );
72  const std::vector<Float_t>& GetRegressionValues();
73 
75 
76  void WriteWeightsToStream(TFile& rf) const;
77  void AddWeightsXMLTo( void* parent ) const;
78  void ReadWeightsFromXML( void* wghtnode );
79 
80  void ReadWeightsFromStream(std::istream& istr);
81  void ReadWeightsFromStream(TFile &rf);
82 
83  const Ranking* CreateRanking();
84 
85  protected:
86 
87  // make ROOT-independent C++ class for classifier response (classifier-specific implementation)
88  void MakeClassSpecific( std::ostream&, const TString& ) const;
89 
90  // get help message text
91  void GetHelpMessage() const;
92 
93  private:
94 
95  // the option handling methods
96  void DeclareOptions();
97  void ProcessOptions();
99 
100  // default initialisation called by all constructors
101  void Init( void );
102 
103  // create kd-tree (binary tree) structure
104  void MakeKNN( void );
105 
106  // polynomial and Gaussian kernel weight function
107  Double_t PolnKernel(Double_t value) const;
108  Double_t GausKernel(const kNN::Event &event_knn, const kNN::Event &event, const std::vector<Double_t> &svec) const;
109 
110  Double_t getKernelRadius(const kNN::List &rlist) const;
111  const std::vector<Double_t> getRMS(const kNN::List &rlist, const kNN::Event &event_knn) const;
112 
113  double getLDAValue(const kNN::List &rlist, const kNN::Event &event_knn);
114 
115  private:
116 
117  // number of events (sumOfWeights)
118  Double_t fSumOfWeightsS; // sum-of-weights for signal training events
119  Double_t fSumOfWeightsB; // sum-of-weights for background training events
120 
121  kNN::ModulekNN *fModule; //! module where all work is done
122 
123  Int_t fnkNN; // number of k-nearest neighbors
124  Int_t fBalanceDepth; // number of binary tree levels used for balancing tree
125 
126  Float_t fScaleFrac; // fraction of events used to compute variable width
127  Float_t fSigmaFact; // scale factor for Gaussian sigma in Gaus. kernel
128 
129  TString fKernel; // ="Gaus","Poln" - kernel type for smoothing
130 
131  Bool_t fTrim; // set equal number of signal and background events
132  Bool_t fUseKernel; // use polynomial kernel weight function
133  Bool_t fUseWeight; // use weights to count kNN
134  Bool_t fUseLDA; // use local linear discriminant analysis to compute MVA
135 
136  kNN::EventVec fEvent; //! (untouched) events used for learning
137 
138  LDA fLDA; //! Experimental feature for local knn analysis
139 
140  // for backward compatibility
141  Int_t fTreeOptDepth; // number of binary tree levels used for optimization
142 
143  ClassDef(MethodKNN,0); // k Nearest Neighbour classifier
144  };
145 
146 } // namespace TMVA
147 
148 #endif // MethodKNN
TMVA::MethodKNN::Train
void Train(void)
kNN training
Definition: MethodKNN.cxx:234
TMVA::MethodKNN::DeclareOptions
void DeclareOptions()
MethodKNN options.
Definition: MethodKNN.cxx:124
TMVA::MethodKNN::fUseKernel
Bool_t fUseKernel
Definition: MethodKNN.h:132
TMVA::MethodBase::ReadWeightsFromStream
virtual void ReadWeightsFromStream(std::istream &)=0
TMVA::MethodKNN::fTrim
Bool_t fTrim
Definition: MethodKNN.h:131
TMVA::Ranking
Ranking for variables in method (implementation)
Definition: Ranking.h:48
TMVA::MethodKNN::MakeClassSpecific
void MakeClassSpecific(std::ostream &, const TString &) const
write specific classifier response
Definition: MethodKNN.cxx:758
ModulekNN.h
Float_t
float Float_t
Definition: RtypesCore.h:57
TMVA::MethodKNN::GausKernel
Double_t GausKernel(const kNN::Event &event_knn, const kNN::Event &event, const std::vector< Double_t > &svec) const
Gaussian kernel.
Definition: MethodKNN.cxx:831
TMVA::MethodKNN::AddWeightsXMLTo
void AddWeightsXMLTo(void *parent) const
write weights to XML
Definition: MethodKNN.cxx:526
TMVA::kNN::ModulekNN
Definition: ModulekNN.h:104
event
Definition: triangle.c:552
TMVA::MethodKNN
Analysis of k-nearest neighbor.
Definition: MethodKNN.h:54
MethodBase.h
TMVA::LDA
Definition: LDA.h:44
TMVA::MethodKNN::Init
void Init(void)
Initialization.
Definition: MethodKNN.cxx:190
TString
Basic string class.
Definition: TString.h:136
bool
TMVA::MethodKNN::MakeKNN
void MakeKNN(void)
create kNN
Definition: MethodKNN.cxx:203
TMVA::MethodKNN::fEvent
kNN::EventVec fEvent
Definition: MethodKNN.h:136
TMVA::MethodKNN::WriteWeightsToStream
void WriteWeightsToStream(TFile &rf) const
save weights to ROOT file
Definition: MethodKNN.cxx:680
TMVA::MethodKNN::fTreeOptDepth
Int_t fTreeOptDepth
Experimental feature for local knn analysis.
Definition: MethodKNN.h:141
TMVA::MethodKNN::getRMS
const std::vector< Double_t > getRMS(const kNN::List &rlist, const kNN::Event &event_knn) const
Get polynomial kernel radius.
Definition: MethodKNN.cxx:893
TMVA::MethodKNN::ReadWeightsFromStream
virtual void ReadWeightsFromStream(std::istream &)=0
TMVA::MethodKNN::fUseWeight
Bool_t fUseWeight
Definition: MethodKNN.h:133
TMVA::MethodKNN::DeclareCompatibilityOptions
void DeclareCompatibilityOptions()
options that are used ONLY for the READER to ensure backward compatibility
Definition: MethodKNN.cxx:140
TMVA::DataSetInfo
Class that contains all the data information.
Definition: DataSetInfo.h:62
TMVA::MethodKNN::fKernel
TString fKernel
Definition: MethodKNN.h:129
TMVA::Types::EAnalysisType
EAnalysisType
Definition: Types.h:128
TMVA::MethodKNN::fSigmaFact
Float_t fSigmaFact
Definition: MethodKNN.h:127
TMVA::MethodKNN::fSumOfWeightsS
Double_t fSumOfWeightsS
Definition: MethodKNN.h:118
TMVA::MethodKNN::PolnKernel
Double_t PolnKernel(Double_t value) const
polynomial kernel
Definition: MethodKNN.cxx:815
TMVA::MethodKNN::GetMvaValue
Double_t GetMvaValue(Double_t *err=0, Double_t *errUpper=0)
Compute classifier response.
Definition: MethodKNN.cxx:296
TMVA::MethodKNN::fBalanceDepth
Int_t fBalanceDepth
Definition: MethodKNN.h:124
TMVA::MethodKNN::HasAnalysisType
virtual Bool_t HasAnalysisType(Types::EAnalysisType type, UInt_t numberClasses, UInt_t numberTargets)
FDA can handle classification with 2 classes and regression with one regression-target.
Definition: MethodKNN.cxx:180
TMVA::MethodKNN::fSumOfWeightsB
Double_t fSumOfWeightsB
Definition: MethodKNN.h:119
TMVA::MethodKNN::fLDA
LDA fLDA
(untouched) events used for learning
Definition: MethodKNN.h:138
TMVA::MethodKNN::GetHelpMessage
void GetHelpMessage() const
get help message text
Definition: MethodKNN.cxx:770
TMVA::MethodBase
Virtual base Class for all MVA method.
Definition: MethodBase.h:111
TMVA::MethodKNN::~MethodKNN
virtual ~MethodKNN(void)
destructor
Definition: MethodKNN.cxx:106
TMVA::MethodKNN::CreateRanking
const Ranking * CreateRanking()
no ranking available
Definition: MethodKNN.cxx:518
TFile
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:54
kNN::EventVec
std::vector< TMVA::kNN::Event > EventVec
Definition: ModulekNN.h:97
unsigned int
Double_t
double Double_t
Definition: RtypesCore.h:59
TMVA::MethodKNN::getKernelRadius
Double_t getKernelRadius(const kNN::List &rlist) const
Get polynomial kernel radius.
Definition: MethodKNN.cxx:869
TMVA::MethodKNN::fScaleFrac
Float_t fScaleFrac
Definition: MethodKNN.h:126
TMVA::MethodKNN::MethodKNN
MethodKNN(const TString &jobName, const TString &methodTitle, DataSetInfo &theData, const TString &theOption="KNN")
standard constructor
Definition: MethodKNN.cxx:62
LDA.h
ClassDef
#define ClassDef(name, id)
Definition: Rtypes.h:325
TMVA::MethodKNN::fModule
kNN::ModulekNN * fModule
Definition: MethodKNN.h:121
kNN
kNN::Event describes point in input variable vector-space, with additional functionality like distanc...
TMVA::MethodKNN::fnkNN
Int_t fnkNN
module where all work is done
Definition: MethodKNN.h:123
kNN::List
std::list< Elem > List
Definition: ModulekNN.h:99
TMVA::MethodKNN::getLDAValue
double getLDAValue(const kNN::List &rlist, const kNN::Event &event_knn)
Definition: MethodKNN.cxx:945
type
int type
Definition: TGX11.cxx:121
TMVA::MethodKNN::ReadWeightsFromXML
void ReadWeightsFromXML(void *wghtnode)
Definition: MethodKNN.cxx:553
TMVA::MethodKNN::GetRegressionValues
const std::vector< Float_t > & GetRegressionValues()
Return vector of averages for target values of k-nearest neighbors.
Definition: MethodKNN.cxx:435
TMVA::kNN::Event
Definition: ModulekNN.h:59
TMVA::MethodKNN::ProcessOptions
void ProcessOptions()
process the options specified by the user
Definition: MethodKNN.cxx:148
TMVA
create variable transformations
Definition: GeneticMinimizer.h:22
int
TMVA::MethodKNN::fUseLDA
Bool_t fUseLDA
Definition: MethodKNN.h:134