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

Source Code for Module pyvision.vector.linearreg

 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  ''' 
35  Implementation of linear regression. 
36  ''' 
37  #import unittest 
38  #import random 
39  import numpy 
40  from numpy import dot,sqrt 
41  from numpy.linalg.linalg import pinv 
42   
43  #TODO: make this a subclass of VectorClassifier 
44 -class LinearReg:
45 - def __init__(self):
46 pass
47
48 - def train_least_squares(self, inputs, outputs):
49 ''' 50 inputs - is a matrix where each row is an input. 51 outputs - is a matrix where each row is a corresponding output. 52 53 based on: http://en.wikipedia.org/wiki/Linear_regression (2007/06/07) 54 ''' 55 self.mat = [] 56 self.RMSE = [] 57 # create the data matrix 58 for output in range(outputs.shape[1]): 59 print "Training output ", output 60 y = outputs[:,output] 61 #print "y:\n",y 62 X = inputs 63 tmp = numpy.ones(shape=(X.shape[0],1)) 64 X = numpy.concatenate([tmp, X],axis=1) 65 #print "X:\n",X 66 B = dot(dot(pinv(dot(X.transpose(),X)),X.transpose()),y) 67 #print "B:\n",B 68 69 E = y - dot(X,B) 70 self.RMSE.append(sqrt((E*E).sum())) 71 #print "E:", E, E < 0.0001 72 self.mat.append( B ) 73 74 self.mat = numpy.array(self.mat) 75 76 return self.RMSE
77
78 - def map(self, data):
79 X = numpy.concatenate([numpy.ones((1,)),data.flatten()]) 80 #print X 81 return dot(self.mat,X)
82
83 - def __call__(self,data):
84 return self.map(data)
85