import numpy as np import numpy.linalg as la class Kernel(object): """Implements list of kernels from http://en.wikipedia.org/wiki/Support_vector_machine """ @staticmethod def linear(): def f(x, y): return np.inner(x, y) return f @staticmethod def gaussian(sigma): def f(x, y): exponent = -np.sqrt(la.norm(x-y) ** 2 / (2 * sigma ** 2)) return np.exp(exponent) return f @staticmethod def _polykernel(dimension, offset): def f(x, y): return (offset + np.dot(x, y)) ** dimension return f @staticmethod def inhomogenous_polynomial(dimension): return Kernel._polykernel(dimension=dimension, offset=1.0) @staticmethod def homogenous_polynomial(dimension): return Kernel._polykernel(dimension=dimension, offset=0.0) @staticmethod def hyperbolic_tangent(kappa, c): def f(x, y): return np.tanh(kappa * np.dot(x, y) + c) return f