classifier.py 1.37 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
"""
    Abstract class for classifiers.
    
    Name: classifier.py
    Author: Alessandro dos Santos Ferreira ( santosferreira.alessandro@gmail.com )
"""

from abc import ABCMeta, abstractmethod

class Classifier(object):
    
    __metaclass__ = ABCMeta
    
    def get_name(self):
        return self.__class__.__name__
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
        
    @staticmethod
    def confusion_matrix(labels, matrix, title = None):
        title = "Confusion Matrix" if title is None else "Confusion Matrix " + title

        info = "===  " + title + " ===\n" 
        info += "\t".join(labels) + "\t<-- classified as\n"
        for i in range(0, len(labels)):
            for val in matrix[i]:
                info += str(int(val)) + "\t"
            info += "| %s\n" % (labels[i])
        
        info += "\n\n"
        
        return info
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
    
    @abstractmethod
    def get_config(self):
        pass
    
    @abstractmethod
    def set_config(self, configs):
        pass
    
    @abstractmethod
    def get_summary_config(self):
        pass
    
    def must_train(self):
        return False
    
50
    def train(self, dataset, training_data, force = False):
51 52 53 54 55
        pass

    @abstractmethod
    def classify(self, dataset, test_data):
        pass
56 57 58 59 60 61 62
    
    def cross_validate(self, detail = True):
        pass

    @abstractmethod
    def reset(self):
        pass