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#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
47namespace 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);
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();
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
int Int_t
Definition: RtypesCore.h:41
unsigned int UInt_t
Definition: RtypesCore.h:42
bool Bool_t
Definition: RtypesCore.h:59
double Double_t
Definition: RtypesCore.h:55
float Float_t
Definition: RtypesCore.h:53
#define ClassDef(name, id)
Definition: Rtypes.h:326
int type
Definition: TGX11.cxx:120
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:48
Class that contains all the data information.
Definition: DataSetInfo.h:60
Definition: LDA.h:44
Virtual base Class for all MVA method.
Definition: MethodBase.h:111
virtual void ReadWeightsFromStream(std::istream &)=0
Analysis of k-nearest neighbor.
Definition: MethodKNN.h:55
void Init(void)
Initialization.
Definition: MethodKNN.cxx:190
Int_t fBalanceDepth
Definition: MethodKNN.h:125
void MakeKNN(void)
create kNN
Definition: MethodKNN.cxx:203
TString fKernel
Definition: MethodKNN.h:130
Float_t fScaleFrac
Definition: MethodKNN.h:127
virtual ~MethodKNN(void)
destructor
Definition: MethodKNN.cxx:106
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
const std::vector< Double_t > getRMS(const kNN::List &rlist, const kNN::Event &event_knn) const
Get polynomial kernel radius.
Definition: MethodKNN.cxx:893
const Ranking * CreateRanking()
no ranking available
Definition: MethodKNN.cxx:518
Int_t fTreeOptDepth
Experimental feature for local knn analysis.
Definition: MethodKNN.h:142
Double_t fSumOfWeightsS
Definition: MethodKNN.h:119
void MakeClassSpecific(std::ostream &, const TString &) const
write specific classifier response
Definition: MethodKNN.cxx:758
MethodKNN(const TString &jobName, const TString &methodTitle, DataSetInfo &theData, const TString &theOption="KNN")
standard constructor
Definition: MethodKNN.cxx:62
void DeclareCompatibilityOptions()
options that are used ONLY for the READER to ensure backward compatibility
Definition: MethodKNN.cxx:140
Double_t getKernelRadius(const kNN::List &rlist) const
Get polynomial kernel radius.
Definition: MethodKNN.cxx:869
void GetHelpMessage() const
get help message text
Definition: MethodKNN.cxx:770
const std::vector< Float_t > & GetRegressionValues()
Return vector of averages for target values of k-nearest neighbors.
Definition: MethodKNN.cxx:435
Double_t fSumOfWeightsB
Definition: MethodKNN.h:120
kNN::EventVec fEvent
Definition: MethodKNN.h:137
void Train(void)
kNN training
Definition: MethodKNN.cxx:234
void ReadWeightsFromStream(std::istream &istr)
read the weights
Definition: MethodKNN.cxx:591
double getLDAValue(const kNN::List &rlist, const kNN::Event &event_knn)
Definition: MethodKNN.cxx:945
Double_t PolnKernel(Double_t value) const
polynomial kernel
Definition: MethodKNN.cxx:815
void ProcessOptions()
process the options specified by the user
Definition: MethodKNN.cxx:148
void ReadWeightsFromXML(void *wghtnode)
Definition: MethodKNN.cxx:553
void AddWeightsXMLTo(void *parent) const
write weights to XML
Definition: MethodKNN.cxx:526
kNN::ModulekNN * fModule
Definition: MethodKNN.h:122
Bool_t fUseLDA
Definition: MethodKNN.h:135
Float_t fSigmaFact
Definition: MethodKNN.h:128
Bool_t fUseWeight
Definition: MethodKNN.h:134
Bool_t fUseKernel
Definition: MethodKNN.h:133
void DeclareOptions()
MethodKNN options.
Definition: MethodKNN.cxx:124
void WriteWeightsToStream(TFile &rf) const
save weights to ROOT file
Definition: MethodKNN.cxx:680
LDA fLDA
(untouched) events used for learning
Definition: MethodKNN.h:139
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
Int_t fnkNN
module where all work is done
Definition: MethodKNN.h:124
Double_t GetMvaValue(Double_t *err=0, Double_t *errUpper=0)
Compute classifier response.
Definition: MethodKNN.cxx:296
Ranking for variables in method (implementation)
Definition: Ranking.h:48
EAnalysisType
Definition: Types.h:127
Basic string class.
Definition: TString.h:131
create variable transformations