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