svm_classifiers.py :  » Math » Modular-toolkit-for-Data-Processing » MDP-2.6 » mdp » contrib » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Math » Modular toolkit for Data Processing 
Modular toolkit for Data Processing » MDP 2.6 » mdp » contrib » svm_classifiers.py
import mdp
from mdp import ClassifierCumulator
from mdp import numx
from itertools import count

"""General routines and base classes for Support Vector Machine classifiers.

    TODO: Implement some scaling. Either by special Scaling Node or internally.
"""


class _LabelNormalizer(object):
    """This class provides a transparent mapping from arbitrary labels
    to a set of well-defined integers.
    
    TODO: This could actually be a node.
    TODO: Needs more refinement. E.g. could automatically round labels to +1, -1
    """
    def __init__(self, labels, mode=None):
        if mode is None:
            mode = "id"
        if mode == "id":
            # don't do anything.
            self.normalize = self._id
            self.revert = self._id
            return
        
        self._mode = mode
        self._labels = set(labels)
        self._mapping = {}
        self._inverse = {}
        if mode == "dual":
            if len(self._labels) > 2:
                msg = "In dual mode only two labels can be given"
                raise mdp.NodeException(msg)
            t_label_norm = zip(self._labels, [1, -1])
            self._set_label_dicts(t_label_norm)
        elif mode == "multi":
            # enumerate from zero to len
            t_label_norm = zip(self._labels, count())
            self._set_label_dicts(t_label_norm)
        else:
            msg = "Remapping mode not known"
            raise mdp.NodeException(msg)
    
    def _set_label_dicts(self, t_label_norm):
        self._mapping = dict(t_label_norm)
        self._inverse = dict((norm, label) for label, norm in t_label_norm)
        
        # check that neither original nor normalised labels have occured more than once
        if not (len(self._mapping) == len(t_label_norm) == len(self._inverse)):
            msg = "Error in label normalisation."
            raise mdp.NodeException(msg) 
    
    def normalize(self, labels):
        return map(self._mapping.get, labels)
    
    def revert(self, norm_labels):
        return map(self._inverse.get, norm_labels)
    
    def _id(self, labels):
        return labels


class _SVMClassifier(ClassifierCumulator):
    """Base class for the SVM classifier nodes."""

    def __init__(self, input_dim = None, dtype = None):
        self.normalizer = None

        super(_SVMClassifier, self).__init__(input_dim, None, dtype)

    def is_invertible(self):
        return False

    def _set_input_dim(self, n):
        self._input_dim = n
        self._output_dim = n

    def _set_output_dim(self, n):
        msg = "Output dim cannot be set explicitly!"
        raise mdp.NodeException(msg)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.