ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 #include <map>
39 
40 // Local
41 #ifndef ROOT_TMVA_MethodBase
42 #include "TMVA/MethodBase.h"
43 #endif
44 #ifndef ROOT_TMVA_ModulekNN
45 #include "TMVA/ModulekNN.h"
46 #endif
47 
48 // SVD and linear discriminat code
49 #ifndef ROOT_TMVA_LDA
50 #include "TMVA/LDA.h"
51 #endif
52 
53 namespace TMVA
54 {
55  namespace kNN
56  {
57  class ModulekNN;
58  }
59 
60  class MethodKNN : public MethodBase
61  {
62  public:
63 
64  MethodKNN(const TString& jobName,
65  const TString& methodTitle,
66  DataSetInfo& theData,
67  const TString& theOption = "KNN",
68  TDirectory* theTargetDir = NULL);
69 
70  MethodKNN(DataSetInfo& theData,
71  const TString& theWeightFile,
72  TDirectory* theTargetDir = NULL);
73 
74  virtual ~MethodKNN( void );
75 
76  virtual Bool_t HasAnalysisType( Types::EAnalysisType type, UInt_t numberClasses, UInt_t numberTargets );
77 
78  void Train( void );
79 
80  Double_t GetMvaValue( Double_t* err = 0, Double_t* errUpper = 0 );
81  const std::vector<Float_t>& GetRegressionValues();
82 
84 
85  void WriteWeightsToStream(TFile& rf) const;
86  void AddWeightsXMLTo( void* parent ) const;
87  void ReadWeightsFromXML( void* wghtnode );
88 
89  void ReadWeightsFromStream(std::istream& istr);
90  void ReadWeightsFromStream(TFile &rf);
91 
92  const Ranking* CreateRanking();
93 
94  protected:
95 
96  // make ROOT-independent C++ class for classifier response (classifier-specific implementation)
97  void MakeClassSpecific( std::ostream&, const TString& ) const;
98 
99  // get help message text
100  void GetHelpMessage() const;
101 
102  private:
103 
104  // the option handling methods
105  void DeclareOptions();
106  void ProcessOptions();
108 
109  // default initialisation called by all constructors
110  void Init( void );
111 
112  // create kd-tree (binary tree) structure
113  void MakeKNN( void );
114 
115  // polynomial and Gaussian kernel weight function
117  Double_t GausKernel(const kNN::Event &event_knn, const kNN::Event &event, const std::vector<Double_t> &svec) const;
118 
119  Double_t getKernelRadius(const kNN::List &rlist) const;
120  const std::vector<Double_t> getRMS(const kNN::List &rlist, const kNN::Event &event_knn) const;
121 
122  double getLDAValue(const kNN::List &rlist, const kNN::Event &event_knn);
123 
124  private:
125 
126  // number of events (sumOfWeights)
127  Double_t fSumOfWeightsS; // sum-of-weights for signal training events
128  Double_t fSumOfWeightsB; // sum-of-weights for background training events
129 
130  kNN::ModulekNN *fModule; //! module where all work is done
131 
132  Int_t fnkNN; // number of k-nearest neighbors
133  Int_t fBalanceDepth; // number of binary tree levels used for balancing tree
134 
135  Float_t fScaleFrac; // fraction of events used to compute variable width
136  Float_t fSigmaFact; // scale factor for Gaussian sigma in Gaus. kernel
137 
138  TString fKernel; // ="Gaus","Poln" - kernel type for smoothing
139 
140  Bool_t fTrim; // set equal number of signal and background events
141  Bool_t fUseKernel; // use polynomial kernel weight function
142  Bool_t fUseWeight; // use weights to count kNN
143  Bool_t fUseLDA; // use local linear discriminat analysis to compute MVA
144 
145  kNN::EventVec fEvent; //! (untouched) events used for learning
146 
147  LDA fLDA; //! Experimental feature for local knn analysis
148 
149  // for backward compatibility
150  Int_t fTreeOptDepth; // number of binary tree levels used for optimization
151 
152  ClassDef(MethodKNN,0) // k Nearest Neighbour classifier
153  };
154 
155 } // namespace TMVA
156 
157 #endif // MethodKNN
void ProcessOptions()
process the options specified by the user
Definition: MethodKNN.cxx:152
Definition: LDA.h:48
Bool_t fUseLDA
Definition: MethodKNN.h:143
void DeclareOptions()
MethodKNN options.
Definition: MethodKNN.cxx:118
float Float_t
Definition: RtypesCore.h:53
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:45
EAnalysisType
Definition: Types.h:124
void Train(void)
kNN training
Definition: MethodKNN.cxx:238
void MakeKNN(void)
create kNN
Definition: MethodKNN.cxx:207
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
Bool_t fUseKernel
Definition: MethodKNN.h:141
void DeclareCompatibilityOptions()
options that are used ONLY for the READER to ensure backward compatibility
Definition: MethodKNN.cxx:144
void ReadWeightsFromStream(std::istream &istr)
read the weights
Definition: MethodKNN.cxx:595
MethodKNN(const TString &jobName, const TString &methodTitle, DataSetInfo &theData, const TString &theOption="KNN", TDirectory *theTargetDir=NULL)
void Init(void)
Initialization.
Definition: MethodKNN.cxx:194
#define ClassDef(name, id)
Definition: Rtypes.h:254
TString fKernel
Definition: MethodKNN.h:138
Double_t PolnKernel(Double_t value) const
polynomial kernel
Definition: MethodKNN.cxx:819
Bool_t fUseWeight
Definition: MethodKNN.h:142
kNN::EventVec fEvent
Definition: MethodKNN.h:145
std::list< Elem > List
Definition: ModulekNN.h:107
std::vector< TMVA::kNN::Event > EventVec
Definition: ModulekNN.h:105
void WriteWeightsToStream(TFile &rf) const
save weights to ROOT file
Definition: MethodKNN.cxx:684
LDA fLDA
(untouched) events used for learning
Definition: MethodKNN.h:147
void MakeClassSpecific(std::ostream &, const TString &) const
write specific classifier response
Definition: MethodKNN.cxx:762
Double_t GetMvaValue(Double_t *err=0, Double_t *errUpper=0)
Compute classifier response.
Definition: MethodKNN.cxx:298
Float_t fSigmaFact
Definition: MethodKNN.h:136
Double_t GausKernel(const kNN::Event &event_knn, const kNN::Event &event, const std::vector< Double_t > &svec) const
Gaussian kernel.
Definition: MethodKNN.cxx:835
virtual ~MethodKNN(void)
destructor
Definition: MethodKNN.cxx:110
Int_t fTreeOptDepth
Experimental feature for local knn analysis.
Definition: MethodKNN.h:150
unsigned int UInt_t
Definition: RtypesCore.h:42
Double_t fSumOfWeightsS
Definition: MethodKNN.h:127
const Ranking * CreateRanking()
no ranking available
Definition: MethodKNN.cxx:522
void ReadWeightsFromXML(void *wghtnode)
Definition: MethodKNN.cxx:557
Int_t fBalanceDepth
Definition: MethodKNN.h:133
double Double_t
Definition: RtypesCore.h:55
Double_t fSumOfWeightsB
Definition: MethodKNN.h:128
Describe directory structure in memory.
Definition: TDirectory.h:44
int type
Definition: TGX11.cxx:120
void AddWeightsXMLTo(void *parent) const
write weights to XML
Definition: MethodKNN.cxx:530
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:184
void GetHelpMessage() const
get help message text
Definition: MethodKNN.cxx:774
const std::vector< Double_t > getRMS(const kNN::List &rlist, const kNN::Event &event_knn) const
Get polynomial kernel radius.
Definition: MethodKNN.cxx:897
#define NULL
Definition: Rtypes.h:82
Float_t fScaleFrac
Definition: MethodKNN.h:135
kNN::ModulekNN * fModule
Definition: MethodKNN.h:130
virtual void ReadWeightsFromStream(std::istream &)=0
const std::vector< Float_t > & GetRegressionValues()
Return vector of averages for target values of k-nearest neighbors.
Definition: MethodKNN.cxx:439
float value
Definition: math.cpp:443
double getLDAValue(const kNN::List &rlist, const kNN::Event &event_knn)
Definition: MethodKNN.cxx:949
Int_t fnkNN
module where all work is done
Definition: MethodKNN.h:132
Double_t getKernelRadius(const kNN::List &rlist) const
Get polynomial kernel radius.
Definition: MethodKNN.cxx:873