Changeset 3714:d496ba15efae in orange
 Timestamp:
 05/30/07 15:51:41 (7 years ago)
 Branch:
 default
 Convert:
 6e906e035c782a7aaa26dfb35ab1496dd72e3694
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

orange/orngRegression.py
r3712 r3714 10 10 11 11 class LinearRegressionLearner(object): 12 """LinearRegressionLearner(data, beta0=True)""" 13 def __new__(self, data=None, name='linear regression', beta0=True, **kwds): 12 def __new__(self, data=None, name='linear regression', **kwds): 14 13 learner = object.__new__(self, **kwds) 15 14 if data: 16 learner.__init__(name =name, beta0=beta0, **kwds) # force init15 learner.__init__(name) # force init 17 16 return learner(data) 18 17 else: 19 18 return learner # invokes the __init__ 20 19 21 def __init__(self, name='linear regression', beta0=True, **kwds): 22 self.__dict__ = kwds 20 def __init__(self, name='linear regression', beta0 = True): 23 21 self.name = name 24 22 self.beta0 = beta0 … … 91 89 self.beta = self.statistics['model']['estCoeff'] 92 90 93 def __call__(self, example , result_type=orange.GetValue):91 def __call__(self, example): 94 92 ex = orange.Example(self.domain, example) 95 93 ex = numpy.array(ex.native()) … … 99 97 else: 100 98 yhat = dot(self.beta, ex[:1]) 101 yhat = orange.Value(yhat)102 dist = 0 # this should be distribution103 99 104 if result_type == orange.GetValue: 105 return yhat 106 if result_type == orange.GetProbabilities: 107 return dist 108 return (yhat, dist) # for orange.GetBoth 100 return yhat 109 101 110 102 def printLinearRegression(lr): … … 144 136 class PLSRegressionLearner(object): 145 137 """PLSRegressionLearner(data, y, x=None, nc=None)""" 146 def __new__(self, data=None, y = None, x = None, nc = None,name='PLS regression', **kwds):138 def __new__(self, data=None, name='PLS regression', **kwds): 147 139 learner = object.__new__(self, **kwds) 148 140 if data: 149 141 learner.__init__(name) # force init 150 return learner(data , y=y, x=x, nc=nc)142 return learner(data) 151 143 else: 152 144 return learner # invokes the __init__ … … 156 148 self.nc = nc 157 149 158 def __call__(self, data, y=None, x=None, nc=None, weight=None): 159 if y == None: 160 if data.domain.classVar == None: 161 import warnings 162 warnings.warn("PLS requires either specification of response variables or a data domain with a class") 163 return None 164 else: 165 y = [data.domain.classVar] 150 def __call__(self, data, y, x=None, nc=None, weight=None): 166 151 if x == None: 167 print y168 152 x = [v for v in data.domain.variables if v not in y] 169 if nc == None:170 nc = len(x)171 153 172 154 dataX = data.select(x) 173 155 dataY = data.select(y) 174 156 print y, dataY 157 175 158 # transformation to numpy arrays 176 159 X = dataX.toNumpy()[0] 160 Y = dataY.toNumpy()[0] 161 162 # data dimensions 177 163 n, mx = numpy.shape(X) 178 179 Y = dataY.toNumpy()[0] 180 181 # if Y is class 182 if Y == None: 183 Y = dataY.toNumpy()[1] 184 Y = Y.reshape((n,1)) 185 186 my = Y.shape[1] 187 164 my = numpy.shape(Y)[1] 188 165 189 166 # Zscores of original matrices … … 192 169 XMean = numpy.mean(X, axis = 0) 193 170 XStd = numpy.std(X, axis = 0) 194 X, 195 196 P = numpy.empty((mx, nc))197 C = numpy.empty((my, nc))198 T = numpy.empty((n, nc))199 U = numpy.empty((n, nc))200 B = numpy.zeros(( nc,nc))201 W = numpy.empty((mx, nc))202 E, F = X,Y171 X,Y = standardize(X), standardize(Y) 172 173 P = numpy.empty((mx,Ncomp)) 174 C = numpy.empty((my,Ncomp)) 175 T = numpy.empty((n,Ncomp)) 176 U = numpy.empty((n,Ncomp)) 177 B = numpy.zeros((Ncomp,Ncomp)) 178 W = numpy.empty((mx,Ncomp)) 179 E,F = X,Y 203 180 204 181 # main algorithm 205 for i in range( nc):182 for i in range(Ncomp): 206 183 u = numpy.random.random_sample((n,1)) 207 184 w = normalize(dot(E.T,u)) … … 232 209 Y = Y*numpy.std(Y, axis = 0)+ YMean 233 210 BPls = dot(dot(numpy.linalg.pinv(P.T),B),C.T) 234 return PLSRegression( x=x, y=y, BPls=BPls, YMean=YMean, YStd=YStd, XMean=XMean, XStd=XStd, W=W, name=self.name)211 return PLSRegression(domain=data.domain, BPls=BPls, YMean=YMean, YStd=YStd, XMean=XMean, XStd=XStd, name=self.name) 235 212 236 213 class PLSRegression: … … 238 215 self.__dict__ = kwds 239 216 240 def __call__(self, example, result_type=orange.GetValue): 241 ex = orange.Example(self.x, example) 242 ex = numpy.array(ex.native()) 243 ex = (ex  self.XMean) / self.XStd 244 yhat = orange.Value(dot(ex, self.BPls) * self.YStd + self.YMean) 245 dist = 0 246 247 if result_type == orange.GetValue: 248 return yhat 249 if result_type == orange.GetProbabilities: 250 return dist 251 return (yhat, dist) # for orange.GetBoth 217 def __call__(self, example): 218 ex = orange.Example(self.domain, example) 219 ex = numpy.array(ex.native()) 220 ex = (ex  self.XMean) / self.XStd 221 yhat = dot(ex, self.BPls) * self.YStd + self.YMean 222 return yhat
Note: See TracChangeset
for help on using the changeset viewer.