Logo ROOT   6.07/09
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 #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 
69  MethodKNN(DataSetInfo& theData,
70  const TString& theWeightFile);
71 
72  virtual ~MethodKNN( void );
73 
74  virtual Bool_t HasAnalysisType( Types::EAnalysisType type, UInt_t numberClasses, UInt_t numberTargets );
75 
76  void Train( void );
77 
78  Double_t GetMvaValue( Double_t* err = 0, Double_t* errUpper = 0 );
79  const std::vector<Float_t>& GetRegressionValues();
80 
82 
83  void WriteWeightsToStream(TFile& rf) const;
84  void AddWeightsXMLTo( void* parent ) const;
85  void ReadWeightsFromXML( void* wghtnode );
86 
87  void ReadWeightsFromStream(std::istream& istr);
88  void ReadWeightsFromStream(TFile &rf);
89 
90  const Ranking* CreateRanking();
91 
92  protected:
93 
94  // make ROOT-independent C++ class for classifier response (classifier-specific implementation)
95  void MakeClassSpecific( std::ostream&, const TString& ) const;
96 
97  // get help message text
98  void GetHelpMessage() const;
99 
100  private:
101 
102  // the option handling methods
103  void DeclareOptions();
104  void ProcessOptions();
105  void DeclareCompatibilityOptions();
106 
107  // default initialisation called by all constructors
108  void Init( void );
109 
110  // create kd-tree (binary tree) structure
111  void MakeKNN( void );
112 
113  // polynomial and Gaussian kernel weight function
114  Double_t PolnKernel(Double_t value) const;
115  Double_t GausKernel(const kNN::Event &event_knn, const kNN::Event &event, const std::vector<Double_t> &svec) const;
116 
117  Double_t getKernelRadius(const kNN::List &rlist) const;
118  const std::vector<Double_t> getRMS(const kNN::List &rlist, const kNN::Event &event_knn) const;
119 
120  double getLDAValue(const kNN::List &rlist, const kNN::Event &event_knn);
121 
122  private:
123 
124  // number of events (sumOfWeights)
125  Double_t fSumOfWeightsS; // sum-of-weights for signal training events
126  Double_t fSumOfWeightsB; // sum-of-weights for background training events
127 
128  kNN::ModulekNN *fModule; //! module where all work is done
129 
130  Int_t fnkNN; // number of k-nearest neighbors
131  Int_t fBalanceDepth; // number of binary tree levels used for balancing tree
132 
133  Float_t fScaleFrac; // fraction of events used to compute variable width
134  Float_t fSigmaFact; // scale factor for Gaussian sigma in Gaus. kernel
135 
136  TString fKernel; // ="Gaus","Poln" - kernel type for smoothing
137 
138  Bool_t fTrim; // set equal number of signal and background events
139  Bool_t fUseKernel; // use polynomial kernel weight function
140  Bool_t fUseWeight; // use weights to count kNN
141  Bool_t fUseLDA; // use local linear discriminat analysis to compute MVA
142 
143  kNN::EventVec fEvent; //! (untouched) events used for learning
144 
145  LDA fLDA; //! Experimental feature for local knn analysis
146 
147  // for backward compatibility
148  Int_t fTreeOptDepth; // number of binary tree levels used for optimization
149 
150  ClassDef(MethodKNN,0); // k Nearest Neighbour classifier
151  };
152 
153 } // namespace TMVA
154 
155 #endif // MethodKNN
Definition: LDA.h:48
Bool_t fUseLDA
Definition: MethodKNN.h:141
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:50
EAnalysisType
Definition: Types.h:128
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:139
#define ClassDef(name, id)
Definition: Rtypes.h:254
TString fKernel
Definition: MethodKNN.h:136
Bool_t fUseWeight
Definition: MethodKNN.h:140
kNN::EventVec fEvent
Definition: MethodKNN.h:143
std::list< Elem > List
Definition: ModulekNN.h:107
void Init(TClassEdit::TInterpreterLookupHelper *helper)
Definition: TClassEdit.cxx:119
std::vector< TMVA::kNN::Event > EventVec
Definition: ModulekNN.h:105
LDA fLDA
(untouched) events used for learning
Definition: MethodKNN.h:145
Float_t fSigmaFact
Definition: MethodKNN.h:134
Int_t fTreeOptDepth
Experimental feature for local knn analysis.
Definition: MethodKNN.h:148
unsigned int UInt_t
Definition: RtypesCore.h:42
Double_t fSumOfWeightsS
Definition: MethodKNN.h:125
Int_t fBalanceDepth
Definition: MethodKNN.h:131
double Double_t
Definition: RtypesCore.h:55
Double_t fSumOfWeightsB
Definition: MethodKNN.h:126
int type
Definition: TGX11.cxx:120
Abstract ClassifierFactory template that handles arbitrary types.
Float_t fScaleFrac
Definition: MethodKNN.h:133
kNN::ModulekNN * fModule
Definition: MethodKNN.h:128
virtual void ReadWeightsFromStream(std::istream &)=0
Int_t fnkNN
module where all work is done
Definition: MethodKNN.h:130