Package pyvision :: Package vector :: Module Polynomial
[hide private]
[frames] | no frames]

Source Code for Module pyvision.vector.Polynomial

  1  # PyVision License 
  2  # 
  3  # Copyright (c) 2006-2008 David S. Bolme 
  4  # All rights reserved. 
  5  # 
  6  # Redistribution and use in source and binary forms, with or without 
  7  # modification, are permitted provided that the following conditions 
  8  # are met: 
  9  #  
 10  # 1. Redistributions of source code must retain the above copyright 
 11  # notice, this list of conditions and the following disclaimer. 
 12  #  
 13  # 2. Redistributions in binary form must reproduce the above copyright 
 14  # notice, this list of conditions and the following disclaimer in the 
 15  # documentation and/or other materials provided with the distribution. 
 16  #  
 17  # 3. Neither name of copyright holders nor the names of its contributors 
 18  # may be used to endorse or promote products derived from this software 
 19  # without specific prior written permission. 
 20  #  
 21  #  
 22  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 23  # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 24  # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
 25  # A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR 
 26  # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
 27  # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 28  # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
 29  # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
 30  # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
 31  # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 32  # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 33   
 34  from pyvision.vector.VectorClassifier import VectorClassifier,TYPE_REGRESSION 
 35  from numpy import array,dot 
 36  from numpy.linalg import lstsq 
 37   
38 -class Polynomial2D(VectorClassifier):
39 - def __init__(self,order=2,**kwargs):
40 #FIXME: DOcument this code 41 ''' 42 This class fits a polynomial to a function of 2 variables. 43 ''' 44 self.order = order 45 self.x = None 46 VectorClassifier.__init__(self,TYPE_REGRESSION,**kwargs)
47 48
49 - def trainClassifer(self,labels,vectors,ilog=None):
50 ''' 51 Train the polynomial. Do not call this function 52 manually, instead call the train function on the super 53 class. 54 ''' 55 #build matrix 56 matrix = [] 57 for each in vectors: 58 if len(each) != 2: 59 raise ValueError("ERROR: Vector length=%d. Polynomial2D only predicts for vectors of length 2."%len(each)) 60 x,y = each 61 matrix.append(self.buildRow(x,y)) 62 63 matrix = array(matrix) 64 labels = array(labels) 65 66 x,resids,rank,s = lstsq(matrix,labels) 67 68 self.x = x 69 self.resids = resids 70 self.rank = rank 71 self.s = s 72 73 if rank != matrix.shape[1]: 74 print "WARNING: Polynomial is not fully constrained."
75 76 77
78 - def buildRow(self,x,y):
79 ''' 80 Method for private use only. 81 ''' 82 row = [1.0] 83 for o in range(1,self.order+1): 84 for i in range(o+1): 85 row.append(float(x)**i*float(y)**(o-i)) 86 return row
87
88 - def predictValue(self,data,ilog=None):
89 ''' 90 Method for private use only. Call super class predict. 91 ''' 92 if len(data) != 2: 93 raise ValueError("Polynomial2D only predicts for vectors of length 2.") 94 95 x,y = data 96 return dot(self.x , self.buildRow(x,y))
97 98 import unittest 99
100 -class _PolyTest(unittest.TestCase):
101 - def test_buildrow(self):
102 poly = Polynomial2D(order=3) 103 row = array(poly.buildRow(2,2)) 104 error = row-array([1.0,2.0,2.0,4.0,4.0,4.0,8.0,8.0,8.0,8.0]) 105 sse = (error*error).sum() 106 self.assert_(sse < 0.001) 107 108 row = array(poly.buildRow(2,1)) 109 error = row-array([1.0,1.0,2.0,1.0,2.0,4.0,1.0,2.0,4.0,8.0]) 110 sse = (error*error).sum() 111 self.assert_(sse < 0.001) 112 113 row = array(poly.buildRow(1,2)) 114 error = row-array([1.0,2.0,1.0,4.0,2.0,1.0,8.0,4.0,2.0,1.0]) 115 sse = (error*error).sum() 116 self.assert_(sse < 0.001)
117
118 - def test_train(self):
119 poly = Polynomial2D(order=2) 120 for x in range(-8,9): 121 for y in range(-8,9): 122 val = -5 + 3*y + 2*x - 4*y*y + 2*y*x+ x*x 123 poly.addTraining(val,[x,y]) 124 poly.train() 125 for x in range(-8,9): 126 for y in range(-8,9): 127 val = -5 + 3*y + 2*x - 4*y*y + 2*y*x+ x*x 128 pred = poly.predict([x,y]) 129 self.assert_(abs(val - pred) < 0.0001)
130