Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
SVKernelMatrix.cxx
Go to the documentation of this file.
1// @(#)root/tmva $Id$
2// Author: Andrzej Zemla
3
4/**********************************************************************************
5 * Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
6 * Package: TMVA *
7 * Class : SVKernelMatrix *
8 * Web : http://tmva.sourceforge.net *
9 * *
10 * Description: *
11 * Implementation *
12 * *
13 * Authors (alphabetical): *
14 * Marcin Wolter <Marcin.Wolter@cern.ch> - IFJ PAN, Krakow, Poland *
15 * Andrzej Zemla <azemla@cern.ch> - IFJ PAN, Krakow, Poland *
16 * (IFJ PAN: Henryk Niewodniczanski Inst. Nucl. Physics, Krakow, Poland) *
17 * *
18 * Minor modification to improve optimisation of kernel values: *
19 * Adrian Bevan <adrian.bevan@cern.ch> - Queen Mary *
20 * University of London, UK *
21 * Tom Stevenson <thomas.james.stevenson@cern.ch> - Queen Mary *
22 * University of London, UK *
23 * *
24 * Copyright (c) 2005: *
25 * CERN, Switzerland *
26 * MPI-K Heidelberg, Germany *
27 * PAN, Krakow, Poland *
28 * *
29 * Redistribution and use in source and binary forms, with or without *
30 * modification, are permitted according to the terms listed in LICENSE *
31 * (http://tmva.sourceforge.net/LICENSE) *
32 **********************************************************************************/
33
34/*! \class TMVA::SVKernelMatrix
35\ingroup TMVA
36Kernel matrix for Support Vector Machine
37*/
38
39#include "TMVA/SVKernelMatrix.h"
40
41#include "TMVA/MsgLogger.h"
42#include "TMVA/SVEvent.h"
44#include "TMVA/Types.h"
45
46#include "RtypesCore.h"
47
48#include <stdexcept>
49
50////////////////////////////////////////////////////////////////////////////////
51/// constructor
52
54 : fSize(0),
55 fKernelFunction(0),
56 fSVKernelMatrix(0),
57 fLogger( new MsgLogger("ResultsRegression", kINFO) )
58{
59}
60
61////////////////////////////////////////////////////////////////////////////////
62/// constructor
63
64TMVA::SVKernelMatrix::SVKernelMatrix( std::vector<TMVA::SVEvent*>* inputVectors, SVKernelFunction* kernelFunction )
65 : fSize(inputVectors->size()),
66 fKernelFunction(kernelFunction),
67 fLogger( new MsgLogger("SVKernelMatrix", kINFO) )
68{
70 try{
71 for (UInt_t i = 0; i < fSize; i++) fSVKernelMatrix[i] = new Float_t[i+1];
72 }catch(...){
73 Log() << kFATAL << "Input data too large. Not enough memory to allocate memory for Support Vector Kernel Matrix. Please reduce the number of input events or use a different method."<<Endl;
74 }
75 // We compute the diagonal and one half of the off diagonal. When reading back we use
76 // the symmetry of i,j to j,i to ensure the correct values are returned.
77 for (UInt_t i = 0; i < fSize; i++) {
78 for (UInt_t j = 0; j <=i; j++) {
79 fSVKernelMatrix[i][j] = fKernelFunction->Evaluate((*inputVectors)[i], (*inputVectors)[j]);
80 }
81 }
82}
83
84////////////////////////////////////////////////////////////////////////////////
85/// destructor
86
88{
89 for (UInt_t i = fSize -1; i > 0; i--) {
90 delete[] fSVKernelMatrix[i];
91 fSVKernelMatrix[i] = 0;
92 }
93 delete[] fSVKernelMatrix;
94 fSVKernelMatrix = 0;
95}
96
97////////////////////////////////////////////////////////////////////////////////
98/// returns a row of the kernel matrix
99
101{
102 Float_t* fLine = NULL;
103 if (line >= fSize) {
104 return NULL;
105 }
106 else {
107 fLine = new Float_t[fSize];
108 for( UInt_t i = 0; i <line; i++)
109 fLine[i] = fSVKernelMatrix[line][i];
110 for( UInt_t i = line; i < fSize; i++)
111 fLine[i] = fSVKernelMatrix[i][line];
112 return fLine;
113 }
114}
115
116////////////////////////////////////////////////////////////////////////////////
117/// returns an element of the kernel matrix
118
120{
121 if (i > j) return fSVKernelMatrix[i][j];
122 else return fSVKernelMatrix[j][i]; // it's symmetric, ;)
123}
size_t fSize
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
float Float_t
Definition RtypesCore.h:57
ostringstream derivative to redirect and format output
Definition MsgLogger.h:57
Kernel for Support Vector Machine.
Float_t Evaluate(SVEvent *ev1, SVEvent *ev2)
UInt_t fSize
matrix size
Float_t GetElement(UInt_t i, UInt_t j)
returns an element of the kernel matrix
SVKernelMatrix()
constructor
Float_t * GetLine(UInt_t)
returns a row of the kernel matrix
Float_t ** fSVKernelMatrix
kernel matrix
SVKernelFunction * fKernelFunction
kernel function
MsgLogger & Log() const
TLine * line
MsgLogger & Endl(MsgLogger &ml)
Definition MsgLogger.h:148