File: oneclass.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 (60 lines) | stat: -rw-r--r-- 1,820 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
from model import LibSvmModel
import libsvm

__all__ = [
    'LibSvmOneClassModel',
    'LibSvmOneClassResults'
    ]

class LibSvmOneClassResults:
    def __init__(self, model, traindataset, kernel, PredictorType):
        modelc = model.contents
        if modelc.param.svm_type != libsvm.ONE_CLASS:
            raise TypeError, '%s is for one-class problems' % \
                str(self.__class__)
        self.rho = modelc.rho[0]
        self.sv_coef = modelc.sv_coef[0][:modelc.l]
        self.predictor = PredictorType(model, traindataset, kernel)

    def predict(self, dataset):
        """
        This function returns a list of boolean values which indicate
        whether the test vectors form part of the distribution.
        """
        return [self.predictor.predict(x) > 0 for x in dataset]

    def predict_values(self, dataset):
        """
        This function returns a list of floating point values which
        indicate whether the test vectors form part of the
        distribution.

        A positive value indicates that the test vector is part of the
        distribution, while a non-positive value indicates that is is
        not.
        """
        return [self.predictor.predict_values(x, 1) for x in dataset]

    def compact(self):
        self.predictor.compact()

class LibSvmOneClassModel(LibSvmModel):
    """
    A model for distribution estimation (one-class SVM).

    See also: Scholkopf, et al. Estimating the Support of a
    High-Dimensional Distribution.
    """

    ResultsType = LibSvmOneClassResults

    def __init__(self, kernel, nu=0.5, **kwargs):
        """
        Parameters:

        - `nu`: XXX
        """
        LibSvmModel.__init__(self, kernel, **kwargs)
        self.nu = nu
        self.param.svm_type = libsvm.ONE_CLASS
        self.param.nu = nu