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

Source Code for Module pycv.cs.ml.reg.reg

 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      'RegressionDataset', 
27      'LinearRegressor', 
28      'train_LinearRegressor' 
29      ] 
30   
31  from numpy import array, zeros, dot, prod 
32  from scipy.linalg import pinv 
33   
34  from pycv.cs.stats import statslist, mean, moment1, correlation, moment11 
35  from pycv import tprint 
36  from pycv.cs.ml import Predictor, Dataset 
37   
38  #------------------------------------------------------------------------------- 
39  # Representation of a regression data set 
40  #------------------------------------------------------------------------------- 
41 -class RegressionDataset(Dataset):
42 - def __init__(self, input_data, output_data, weights = None ):
43 Dataset.__init__(self,len(input_data)) 44 self.ishape = input_data[0].shape 45 self.isize = prod(self.ishape) 46 self.oshape = output_data[0].shape 47 self.osize = prod(self.oshape) 48 self.input_data = input_data 49 self.output_data = output_data 50 self.weights = weights
51 52 53 #------------------------------------------------------------------------------- 54 # Linear Regressor 55 #-------------------------------------------------------------------------------
56 -class LinearRegressor(Predictor):
57 - def __init__( self, ishape, oshape, A = None, b = None):
58 self.ishape = ishape 59 self.oshape = oshape 60 if A is None: 61 self.A = zeros(ishape+oshape) 62 else: 63 self.A = A 64 if b is None: 65 self.b = zeros(oshape) 66 else: 67 self.b = b
68
69 - def predict(self, input_point, *args, **kwds):
70 return (dot(self.A,input_point.ravel())+self.b).reshape(self.oshape)
71 72
73 -def train_LinearRegressor( regression_dataset ):
74 """Train a LinearRegressor using a RegressionDataset 75 76 Input: 77 regression_dataset: a RegressionDataset 78 Output: 79 a LinearRegressor 80 """ 81 82 rd = regression_dataset 83 idata = rd.input_data.reshape(rd.N,rd.isize) 84 odata = rd.output_data.reshape(rd.N,rd.osize) 85 86 n, m1, cov1 = statslist(idata,rd.weights) 87 m2 = mean(n,moment1(odata,weights)) 88 cor = correlation(n,moment11(idata,odata,weights),m1,m2) 89 90 self.A = dot(pinv(cov1),cor).T 91 self.b = m2 - dot(self.A,m1) 92 93 return LinearRegressor(rd.ishape,rd.oshape,A,b)
94