Changeset 10369:3d067903408f in orange for Orange/classification/svm/kernels.py
 Timestamp:
 02/25/12 22:37:01 (2 years ago)
 Branch:
 default
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

Orange/classification/svm/kernels.py
r9671 r10369 14 14 """A base class for kernel function wrappers. 15 15 16 :param wrapped: a function to wrap 17 :type wrapped: function(:class:`Orange.data.Instance`, :class:`Orange.data.Instance`) 16 :param wrapped: a kernel function to wrap 18 17 19 18 """ … … 22 21 self.wrapped=wrapped 23 22 24 def __call__(self, example1, example2):25 return self.wrapped( example1, example2)23 def __call__(self, inst1, inst2): 24 return self.wrapped(inst1, inst2) 26 25 27 26 class DualKernelWrapper(KernelWrapper): 28 27 29 """A base class for kernel wrapper that wraps two otherkernel functions.28 """A base class for kernel wrapper that wraps two kernel functions. 30 29 31 :param wrapped1: a function to wrap 32 :type wrapped1: function(:class:`Orange.data.Instance`, :class:`Orange.data.Instance`) 33 :param wrapped2: a function to wrap 34 :type wrapped2: function(:class:`Orange.data.Instance`, :class:`Orange.data.Instance`) 30 :param wrapped1: first kernel function 31 :param wrapped2: second kernel function 35 32 36 33 """ … … 42 39 class RBFKernelWrapper(KernelWrapper): 43 40 44 """A Kernel wrapper that uses a wrapped kernel function in a RBF 45 (Radial Basis Function). 41 """A Kernel wrapper that wraps the given function into RBF 46 42 47 :param wrapped: a function to wrap 48 :type wrapped: function(:class:`Orange.data.Instance`, :class:`Orange.data.Instance`) 43 :param wrapped: a kernel function 49 44 :param gamma: the gamma of the RBF 50 45 :type gamma: double … … 56 51 self.gamma=gamma 57 52 58 def __call__(self, example1, example2): 59 """:math:`exp(gamma * wrapped(example1, example2) ^ 2)` 60 53 def __call__(self, inst1, inst2): 54 """Return :math:`exp(gamma * wrapped(inst1, inst2) ^ 2)` 61 55 """ 62 56 63 return math.exp( self.gamma*math.pow(self.wrapped(example1,64 example2),2))57 return math.exp( 58 self.gamma*math.pow(self.wrapped(inst1, inst2), 2)) 65 59 66 60 class PolyKernelWrapper(KernelWrapper): … … 68 62 """Polynomial kernel wrapper. 69 63 70 :param wrapped: a function to wrap71 :type wrapped: function(:class:`Orange.data.Instance`, :class:`Orange.data.Instance`) 64 :param wrapped: a kernel function 65 72 66 :param degree: degree of the polynomial 73 :type degree: double67 :type degree: float 74 68 75 69 """ … … 79 73 self.degree=degree 80 74 81 def __call__(self, example1, example2):82 """ :math:`wrapped(example1, example2) ^ d`"""75 def __call__(self, inst1, inst2): 76 """Return :math:`wrapped(inst1, inst2) ^ d`""" 83 77 84 return math.pow(self.wrapped( example1, example2), self.degree)78 return math.pow(self.wrapped(inst1, inst2), self.degree) 85 79 86 80 class AdditionKernelWrapper(DualKernelWrapper): 87 81 88 """Addition kernel wrapper.""" 82 """ 83 Addition kernel wrapper. 84 85 :param wrapped1: first kernel function 86 :param wrapped2: second kernel function 87 88 """ 89 89 90 def __call__(self, example1, example2):91 """ :math:`wrapped1(example1, example2) + wrapped2(example1, example2)`90 def __call__(self, inst1, inst2): 91 """Return :math:`wrapped1(inst1, inst2) + wrapped2(inst1, inst2)` 92 92 93 93 """ 94 94 95 return self.wrapped1(example1, example2) + \ 96 self.wrapped2(example1, example2) 95 return self.wrapped1(inst1, inst2) + self.wrapped2(inst1, inst2) 97 96 98 97 class MultiplicationKernelWrapper(DualKernelWrapper): 99 98 100 """Multiplication kernel wrapper.""" 99 """ 100 Multiplication kernel wrapper. 101 102 :param wrapped1: first kernel function 103 :param wrapped2: second kernel function 104 """ 101 105 102 def __call__(self, example1, example2):103 """ :math:`wrapped1(example1, example2) * wrapped2(example1, example2)`106 def __call__(self, inst1, inst2): 107 """Return :math:`wrapped1(inst1, inst2) * wrapped2(inst1, inst2)` 104 108 105 109 """ 106 110 107 return self.wrapped1(example1, example2) * \ 108 self.wrapped2(example1, example2) 111 return self.wrapped1(inst1, inst2) * self.wrapped2(inst1, inst2) 109 112 110 113 class CompositeKernelWrapper(DualKernelWrapper): 111 114 112 115 """Composite kernel wrapper. 113 114 :param wrapped1: a function to wrap 115 :type wrapped1: function(:class:`Orange.data.Instance`, :class:`Orange.data.Instance`) 116 :param wrapped2: a function to wrap 117 :type wrapped2: function(:class:`Orange.data.Instance`, :class:`Orange.data.Instance`) 116 117 :param wrapped1: first kernel function 118 :param wrapped2: second kernel function 118 119 :param l: coefficient 119 120 :type l: double … … 125 126 self.l=l 126 127 127 def __call__(self, example1, example2):128 """ :math:`l*wrapped1(example1,example2)+(1l)*wrapped2(example1,example2)`128 def __call__(self, inst1, inst2): 129 """Return :math:`l*wrapped1(inst1, inst2) + (1l)*wrapped2(inst1, inst2)` 129 130 130 131 """ 131 return self.l * self.wrapped1( example1, example2) + (1self.l) *\132 self.wrapped2(example1,example2)132 return self.l * self.wrapped1(inst1, inst2) + \ 133 (1self.l) * self.wrapped2(inst1, inst2) 133 134 134 135 class SparseLinKernel(object): 135 def __call__(self, example1, example2): 136 """Computes a linear kernel function using the examples meta attributes 137 (need to be floats). 136 def __call__(self, inst1, inst2): 137 """ 138 Compute a linear kernel function using the instances' meta attributes. 139 The meta attributes' values must be floats. 138 140 139 141 """ 140 s = set( example1.getmetas().keys()) & set(example2.getmetas().keys())142 s = set(inst1.getmetas().keys()) & set(inst2.getmetas().keys()) 141 143 sum = 0 142 144 for key in s: 143 sum += float( example2[key]) * float(example1[key])145 sum += float(inst2[key]) * float(inst1[key]) 144 146 return sum 145 147
Note: See TracChangeset
for help on using the changeset viewer.