File: kernel.py

package info (click to toggle)
python-scipy 0.6.0-12
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 32,016 kB
  • ctags: 46,675
  • sloc: cpp: 124,854; ansic: 110,614; python: 108,664; fortran: 76,260; objc: 424; makefile: 384; sh: 10
file content (120 lines) | stat: -rw-r--r-- 3,176 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import numpy as N

import libsvm

__all__ = [
    'LinearKernel',
    'PolynomialKernel',
    'RBFKernel',
    'SigmoidKernel',
    'CustomKernel'
    ]

class LinearKernel:
    """Linear kernel: u'*v."""
    def __init__(self):
        self.kernel_type = libsvm.LINEAR

    def __call__(self, x, y):
        x = N.atleast_2d(x)
        y = N.atleast_2d(y)
        return N.dot(x, y.T)

    def compact(self, *args):
        return self

class PolynomialKernel:
    """Polynomial kernel: (gamma*u'*v + coef0)^degree."""

    def __init__(self, degree, gamma, coef0):
        self.kernel_type = libsvm.POLY
        self.degree = degree
        self.gamma = gamma
        self.coef0 = coef0

    def __call__(self, x, y):
        x = N.atleast_2d(x)
        y = N.atleast_2d(y)
        base = self.gamma * N.dot(x, y.T) + self.coef0
        tmp = base
        ret = 1.0
        t = self.degree
        while t > 0:
            if t % 2 == 1: ret *= tmp
            tmp *= tmp
            t /= 2
        return ret

    def __repr__(self):
        return '<PolynomialKernel: degree=%d, gamma=%.4f, coef0=%.4f>' % \
            (self.degree, self.gamma, self.coef0)

    def compact(self, *args):
        raise NotImplementedError, \
            'model compaction for PolynomialKernel not implemented'

class RBFKernel:
    """Radial basis function kernel: exp(-gamma*|u-v|^2)"""

    def __init__(self, gamma):
        self.kernel_type = libsvm.RBF
        self.gamma = gamma
        self.__call__ = self.evaluate

    def evaluate(self, x, y):
        x = N.atleast_2d(x)
        y = N.atleast_2d(y)
        xnorm = N.atleast_2d(N.sum(x*x, axis=1))
        ynorm = N.atleast_2d(N.sum(y*y, axis=1))
        z = xnorm + ynorm - 2 * N.atleast_2d(N.dot(x, y.T).squeeze())
        return N.exp(-self.gamma * z)

    def evaluate_compact(self, x, y):
        raise NotImplementedError

    def __repr__(self):
        return '<RBFKernel: gamma=%.4f>' % (self.gamma,)

    def compact(self, *args):
        raise NotImplementedError, \
            'model compaction for RBFKernel not implemented'

class SigmoidKernel:
    """Sigmoid kernel: tanh(gamma*u'*v + coef0)"""

    def __init__(self, gamma, coef0):
        self.kernel_type = libsvm.SIGMOID
        self.gamma = gamma
        self.coef0 = coef0

    def __call__(self, x, y):
        x = N.atleast_2d(x)
        y = N.atleast_2d(y)
        return N.tanh(self.gamma * N.dot(x, y.T) + self.coef0)

    def __repr__(self):
        return '<SigmoidKernel: gamma=%.4f, coef0=%.4f>' % \
            (self.gamma, self.coef0)

    def compact(self, *args):
        raise NotImplementedError, \
            'model compaction for SigmoidKernel not implemented'

class CustomKernel:
    """Custom kernel: any callable"""

    def __init__(self, f):
        self.kernel_type = libsvm.PRECOMPUTED
        self.f = f

    def __call__(self, x, y):
        x = N.atleast_2d(x)
        y = N.atleast_2d(y)
        return self.f(x, y)

    def __repr__(self):
        return '<CustomKernel: %s>' % str(self.f)

    def compact(self, *args):
        raise NotImplementedError, \
            'model compaction for CustomKernel not implemented'