Changeset 9523:d1bed8eab680 in orange for orange/Orange/regression/pls.py
 Timestamp:
 01/09/12 17:41:08 (2 years ago)
 Branch:
 default
 Convert:
 c228716ea07062e32d54b33dee5b05d4d3c443fd
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

orange/Orange/regression/pls.py
r9248 r9523 226 226 if x_vars is None and y_vars is None: 227 227 # Response variables are defined in the table. 228 label_mask = data_label_mask(domain) 229 multilabel_flag = (sum(label_mask)  (1 if domain.class_var else 0)) > 0 230 x_vars = [v for v, label in zip(domain, label_mask) if not label] 231 y_vars = [v for v, label in zip(domain, label_mask) if label] 228 x_vars = domain.features 229 if domain.class_var: 230 y_vars = [domain.class_var] 231 multitarget = False 232 elif domain.class_vars: 233 y_vars = domain.class_vars 234 multitarget = True 235 else: 236 raise TypeError('Classless domain (xvars and yvars needed).') 232 237 x_table = select_attrs(table, x_vars) 233 238 y_table = select_attrs(table, y_vars) 234 235 239 elif x_vars and y_vars: 236 240 # independent and response variables are passed by the caller 237 if domain.class_var and domain.class_var not in y_vars: 238 # if the original table contains class variable 239 # add it to the y_vars 240 y_vars.append(domain.class_var) 241 label_mask = [v in y_vars for v in domain.variables] 242 multilabel_flag = True 243 x_table = select_attrs(table, x_vars) 244 y_table = select_attrs(table, y_vars) 241 multitarget = True 245 242 else: 246 243 raise ValueError("Both x_vars and y_vars must be defined.") 244 245 x_table = select_attrs(table, x_vars) 246 y_table = select_attrs(table, y_vars) 247 247 248 248 # dicrete values are continuized … … 258 258 259 259 domain = Orange.data.Domain(x_vars + y_vars, False) 260 label_mask = [False for _ in x_vars] + [True for _ in y_vars] 261 262 x = x_table.toNumpy()[0] 263 y = y_table.toNumpy()[0] 260 261 x = x_table.to_numpy()[0] 262 y = y_table.to_numpy()[0] 264 263 265 264 kwargs = self.fit(x, y) 266 return PLSRegression(label_mask=label_mask, domain=domain, \ 267 # coefs=self.coefs, muX=self.muX, muY=self.muY, \ 268 # sigmaX=self.sigmaX, sigmaY=self.sigmaY, \ 269 x_vars=x_vars, y_vars=y_vars, 270 multilabel_flag=multilabel_flag, **kwargs) 265 return PLSRegression(domain=domain, x_vars=x_vars, y_vars=y_vars, 266 **kwargs) 271 267 272 268 def fit(self, X, Y): … … 401 397 402 398 """ 403 def __init__(self, label_mask=None, domain=None, \ 404 coefs=None, mu_x=None, mu_y=None, sigma_x=None, sigma_y=None, \ 405 x_vars=None, y_vars=None, multilabel_flag=0, **kwargs): 406 self.label_mask = label_mask 399 def __init__(self, domain=None, multitarget=False, coefs=None, sigma_x=None, sigma_y=None, 400 mu_x=None, mu_y=None, x_vars=None, y_vars=None, **kwargs): 407 401 self.domain = domain 402 self.multitarget = multitarget 408 403 self.coefs = coefs 409 404 self.mu_x, self.mu_y = mu_x, mu_y 410 405 self.sigma_x, self.sigma_y = sigma_x, sigma_y 411 406 self.x_vars, self.y_vars = x_vars, y_vars 412 self.multilabel_flag = multilabel_flag413 if not multilabel_flag and y_vars:414 self.class_var = y_vars[0]415 407 416 408 for name, val in kwargs.items(): … … 435 427 y_hat = [var(val) for var, val in zip(self.y_vars, predicted)] 436 428 if result_type == Orange.core.GetValue: 437 return y_hat if self.multi label_flagelse y_hat[0]429 return y_hat if self.multitarget else y_hat[0] 438 430 else: 439 431 from Orange.statistics.distribution import Distribution … … 444 436 probs.append(dist) 445 437 if result_type == Orange.core.GetBoth: 446 return (y_hat, probs) if self.multi label_flagelse (y_hat[0], probs[0])438 return (y_hat, probs) if self.multitarget else (y_hat[0], probs[0]) 447 439 else: 448 return probs if self.multi label_flagelse probs[0]440 return probs if self.multitarget else probs[0] 449 441 450 def print_pls_regression_coefficients(self):442 def to_string(self): 451 443 """ Prettyprints the coefficient of the PLS regression model. 452 444 """ 453 445 x_vars, y_vars = [x.name for x in self.x_vars], [y.name for y in self.y_vars] 454 print " " * 7 + "%6s " * len(y_vars) % tuple(y_vars)455 446 fmt = "%6s " + "%5.3f " * len(y_vars) 456 for i, coef in enumerate(self.coefs): 457 print fmt % tuple([x_vars[i]] + list(coef)) 447 first = [" " * 7 + "%6s " * len(y_vars) % tuple(y_vars)] 448 lines = [fmt % tuple([x_vars[i]] + list(coef)) 449 for i, coef in enumerate(self.coefs)] 450 return '\n'.join(first + lines) 458 451 452 def __str__(self): 453 return self.to_string() 454 459 455 """ 460 456 def transform(self, X, Y=None): … … 487 483 from Orange.regression import pls 488 484 489 table= Orange.data.Table("testpls.tab")485 data = Orange.data.Table("testpls.tab") 490 486 l = pls.PLSRegressionLearner() 491 487 492 x = [var for var in table.domainif var.name[0]=="X"]493 y = [var for var in table.domainif var.name[0]=="Y"]488 x = [var for var in data.domain.features if var.name[0]=="X"] 489 y = [var for var in data.domain.class_vars if var.name[0]=="Y"] 494 490 print x, y 495 # c = l(table, x_vars=x, y_vars=y) 496 c = l(table) 497 c.print_pls_regression_coefficients() 491 # c = l(data, x_vars=x, y_vars=y) 492 c = l(data) 493 494 print c
Note: See TracChangeset
for help on using the changeset viewer.