Home | Trees | Indices | Help |
---|
|
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 3797 98 import unittest 9940 #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 4850 ''' 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 7779 ''' 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 row87130102 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)117119 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)
Home | Trees | Indices | Help |
---|
Generated by Epydoc 3.0.1 on Tue Apr 21 14:31:59 2015 | http://epydoc.sourceforge.net |