/usr/share/pyshared/mvpa2/kernels/libsvm.py is in python-mvpa2 2.2.0-4ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
#
# See COPYING file distributed along with the PyMVPA package for the
# copyright and license terms.
#
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
"""PyMVPA LibSVM-based kernels
These kernels do not currently have the ability to run the calculations, so
they are not translateable to other kernel types. They are implemented solely
to standardize the interface between other kernel machines.
"""
__docformat__ = 'restructuredtext'
from mvpa2.kernels.base import Kernel
from mvpa2.base.param import Parameter
#from mvpa2.clfs.libsvmc import _svmc
class _svmc(object):
"""Locally defining constants for LibSVM to avoid circular import.
"""
LINEAR = 0
POLY = 1
RBF = 2
SIGMOID = 3
class LSKernel(Kernel):
"""A Kernel object which dictates how LibSVM will calculate the kernel"""
def __init__(self, *args, **kwargs):
"""Base class for LIBSVM Kernels has no parameters
"""
Kernel.__init__(self, *args, **kwargs)
self.compute()
def compute(self, *args, **kwargs):
self._k = self.__kernel_type__ # Nothing to compute
def as_raw_ls(self):
return self._k
def as_ls(self):
return self
def as_raw_np(self):
raise ValueError, 'LibSVM calculates kernels internally; they ' +\
'cannot be converted to Numpy'
# Conversion methods
def _as_ls(kernel):
raise NotImplemented, 'LibSVM calculates kernels internally; they ' +\
'cannot be converted from Numpy'
def _as_raw_ls(kernel):
raise NotImplemented, 'LibSVM calculates kernels internally; they ' +\
'cannot be converted from Numpy'
Kernel.add_conversion('ls', _as_ls, _as_raw_ls)
class LinearLSKernel(LSKernel):
"""A simple Linear kernel: K(a,b) = a*b.T"""
__kernel_type__ = _svmc.LINEAR
__kernel_name__ = 'linear'
class RbfLSKernel(LSKernel):
"""Radial Basis Function kernel (aka Gaussian):
K(a,b) = exp(-gamma*||a-b||**2)
"""
__kernel_type__ = _svmc.RBF
__kernel_name__ = 'rbf'
gamma = Parameter(1, doc='Gamma multiplying paramater for Rbf')
def __init__(self, **kwargs):
# Necessary for proper docstring construction
LSKernel.__init__(self, **kwargs)
class PolyLSKernel(LSKernel):
"""Polynomial kernel: K(a,b) = (gamma*a*b.T + coef0)**degree"""
__kernel_type__ = _svmc.POLY
__kernel_name__ = 'poly'
gamma = Parameter(1, doc='Gamma multiplying parameter for Polynomial')
degree = Parameter(2, doc='Degree of polynomial')
coef0 = Parameter(1, doc='Offset inside polynomial') # aka coef0
def __init__(self, **kwargs):
# Necessary for proper docstring construction
LSKernel.__init__(self, **kwargs)
class SigmoidLSKernel(LSKernel):
"""Sigmoid kernel: K(a,b) = tanh(gamma*a*b.T + coef0)"""
__kernel_type__ = _svmc.SIGMOID
__kernel_name__ = 'sigmoid'
gamma = Parameter(1, doc='Gamma multiplying parameter for SigmoidKernel')
coef0 = Parameter(1, doc='Offset inside tanh')
def __init__(self, **kwargs):
# Necessary for proper docstring construction
LSKernel.__init__(self, **kwargs)
|