Package pycv :: Package cs :: Package ml :: Module ml
[hide private]
[frames] | no frames]

Source Code for Module pycv.cs.ml.ml

  1  # PyCV - A Computer Vision Package for Python Incorporating Fast Training of Face Detection 
  2   
  3  # Copyright 2007 Nanyang Technological University, Singapore. 
  4  # Authors: Minh-Tri Pham, Viet-Dung D. Hoang, and Tat-Jen Cham. 
  5   
  6  # This file is part of PyCV. 
  7   
  8  # PyCV is free software: you can redistribute it and/or modify 
  9  # it under the terms of the GNU General Public  
 10  # License as published by the Free Software Foundation, either version  
 11  # 3 of the License, or (at your option) any later version. 
 12   
 13  # PyCV is distributed in the hope that it will be useful, 
 14  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 16  # GNU General Public License for more details. 
 17   
 18  # You should have received a copy of the GNU General Public License 
 19  # along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 20   
 21  # --------------------------------------------------------------------- 
 22  #!/usr/bin/env python 
 23   
 24   
 25  __all__ = [ 
 26      'Predictor', 
 27      'Dataset', 
 28      'DataGenerator', 
 29      'OnlineLearningInterface',  
 30      'PredictPdfInterface', 
 31      ] 
 32   
 33  from numpy import array 
 34   
 35   
 36  #------------------------------------------------------------------------------- 
 37  # Representation of a data set 
 38  #------------------------------------------------------------------------------- 
39 -class Dataset:
40 - def __init__(self,N):
41 self.N = N
42 43 44 #------------------------------------------------------------------------------- 45 # Representation of a data generator 46 #-------------------------------------------------------------------------------
47 -class DataGenerator:
48 - def __init__(self,input_shape):
49 self.input_shape = input_shape
50 51 52 #------------------------------------------------------------------------------- 53 # Machine-learning predictor (a filter, a classifier, or a regressor) 54 #-------------------------------------------------------------------------------
55 -class Predictor:
56
57 - class EarlyPrediction(Exception):
58 """EarlyPrediction 59 60 An EarlyPrediction exception is raised when the output can be 61 predicted early. 62 """
63 - def __init__(self, output, *args, **kwds):
64 self.output = output 65 self.args = args 66 self.kwds = kwds
67
68 - def __str__(self):
69 return repr([self.output, self.args, self.kwds])
70 71
72 - def predict(self, input_point, *args, **kwds):
73 """Predict the output outcome of an input point.""" 74 raise NotImplementedError, "Method predict() has not been implemented."
75
76 - def test(self, input_data, *args, **kwds):
77 """Predict the output outcomes of an array of input points.""" 78 return array([self.predict(x, *args, **kwds) for x in input_data])
79 80 81 #------------------------------------------------------------------------------- 82 # Probabilistic Interface 83 #-------------------------------------------------------------------------------
84 -class PredictPdfInterface:
85
86 - def predict_pdf(self, input_point, *args, **kwds):
87 """Predict the output class pdf given an input point.""" 88 raise NotImplementedError, "Method predict_pdf() has not been implemented."
89
90 - def predict(self, input_point, *args, **kwds):
91 """Predict the output outcome of an input point.""" 92 return self.predict_pdf(input_point, *args, **kwds).argmax()
93
94 - def test_pdf(self, input_data, *args, **kwds):
95 return array([self.predict_pdf(x, *args, **kwds) for x in input_data])
96 97 98 #------------------------------------------------------------------------------- 99 # Online Learning Interface 100 #-------------------------------------------------------------------------------
101 -class OnlineLearningInterface:
102
103 - def learn(self, input_point, *args, **kwds):
104 """Learn incrementally with a new input point.""" 105 raise NotImplementedError, "Method learn() has not been implemented."
106