Logo ROOT   6.18/05
Reference Guide
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 <iostream>
49#include <stdexcept>
50
51////////////////////////////////////////////////////////////////////////////////
52/// constructor
53
55 : fSize(0),
56 fKernelFunction(0),
57 fSVKernelMatrix(0),
58 fLogger( new MsgLogger("ResultsRegression", kINFO) )
59{
60}
61
62////////////////////////////////////////////////////////////////////////////////
63/// constructor
64
65TMVA::SVKernelMatrix::SVKernelMatrix( std::vector<TMVA::SVEvent*>* inputVectors, SVKernelFunction* kernelFunction )
66 : fSize(inputVectors->size()),
67 fKernelFunction(kernelFunction),
68 fLogger( new MsgLogger("SVKernelMatrix", kINFO) )
69{
71 try{
72 for (UInt_t i = 0; i < fSize; i++) fSVKernelMatrix[i] = new Float_t[i+1];
73 }catch(...){
74 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;
75 }
76 // We compute the diagonal and one half of the off diagonal. When reading back we use
77 // the symmetry of i,j to j,i to ensure the correct values are returned.
78 for (UInt_t i = 0; i < fSize; i++) {
79 for (UInt_t j = 0; j <=i; j++) {
80 fSVKernelMatrix[i][j] = fKernelFunction->Evaluate((*inputVectors)[i], (*inputVectors)[j]);
81 }
82 }
83}
84
85////////////////////////////////////////////////////////////////////////////////
86/// destructor
87
89{
90 for (UInt_t i = fSize -1; i > 0; i--) {
91 delete[] fSVKernelMatrix[i];
92 fSVKernelMatrix[i] = 0;
93 }
94 delete[] fSVKernelMatrix;
95 fSVKernelMatrix = 0;
96}
97
98////////////////////////////////////////////////////////////////////////////////
99/// returns a row of the kernel matrix
100
102{
103 Float_t* fLine = NULL;
104 if (line >= fSize) {
105 return NULL;
106 }
107 else {
108 fLine = new Float_t[fSize];
109 for( UInt_t i = 0; i <line; i++)
110 fLine[i] = fSVKernelMatrix[line][i];
111 for( UInt_t i = line; i < fSize; i++)
112 fLine[i] = fSVKernelMatrix[i][line];
113 return fLine;
114 }
115}
116
117////////////////////////////////////////////////////////////////////////////////
118/// returns an element of the kernel matrix
119
121{
122 if (i > j) return fSVKernelMatrix[i][j];
123 else return fSVKernelMatrix[j][i]; // it's symmetric, ;)
124}
unsigned int UInt_t
Definition: RtypesCore.h:42
float Float_t
Definition: RtypesCore.h:53
ostringstream derivative to redirect and format output
Definition: MsgLogger.h:59
Kernel for Support Vector Machine.
Float_t Evaluate(SVEvent *ev1, SVEvent *ev2)
Float_t GetElement(UInt_t i, UInt_t j)
returns an element of the kernel matrix
SVKernelMatrix()
constructor
~SVKernelMatrix()
destructor
Float_t * GetLine(UInt_t)
returns a row of the kernel matrix
Float_t ** fSVKernelMatrix
SVKernelFunction * fKernelFunction
MsgLogger & Log() const
message logger
TLine * line
MsgLogger & Endl(MsgLogger &ml)
Definition: MsgLogger.h:158