Changeset 9521:4a6752f76713 in orange
 Timestamp:
 01/09/12 15:44:53 (2 years ago)
 Branch:
 default
 Convert:
 3a890e197a60d8ae64349affa0cd6e22c5b441ff
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

orange/Orange/regression/lasso.py
r9290 r9521 15 15 >>> table = Orange.data.Table("housing") 16 16 >>> c = lasso.LassoRegressionLearner(table) 17 >>> linear.print_lasso_regression_model(c)17 >>> print c 18 18 19 19 Variable Coeff Est Std Error p 20 20 Intercept 22.533 21 CRIM 0.049 0.282 0.770 22 ZN 0.106 0.055 0.030 * 23 INDUS 0.111 0.442 0.920 24 CHAS 1.757 0.669 0.180 25 NOX 0.318 0.483 0.680 26 RM 1.643 0.461 0.480 27 AGE 0.062 0.051 0.230 28 DIS 0.627 0.538 0.930 29 RAD 1.260 0.472 0.070 . 30 TAX 0.074 0.027 0.120 31 PTRATIO 1.331 0.464 0.050 . 32 B 0.017 0.007 0.080 . 33 LSTAT 0.209 0.323 0.650 21 CRIM 0.044 0.030 0.510 22 ZN 0.013 0.010 0.660 23 INDUS 0.003 0.023 0.980 24 CHAS 2.318 1.304 0.200 25 NOX 7.530 2.803 0.370 26 RM 4.231 0.819 0.000 *** 27 DIS 0.710 0.130 0.070 . 28 RAD 0.074 0.029 0.510 29 TAX 0.004 0.002 0.560 30 PTRATIO 0.821 0.095 0.000 *** 31 B 0.007 0.002 0.170 32 LSTAT 0.503 0.085 0.000 *** 34 33 Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 empty 1 35 34 36 35 37 All variables have nonzero regression coefficients. 36 For 1 variable the regression coefficient equals 0: 37 AGE 38 38 39 39 >>> … … 332 332 return dist 333 333 else: 334 return (y_hat, dist) 334 return (y_hat, dist) 335 336 @deprecated_keywords({"skipZero": "skip_zero"}) 337 def to_string(self, skip_zero=True): 338 """Prettyprints Lasso regression model, 339 i.e. estimated regression coefficients with standard errors 340 and significances. Standard errors are obtained using bootstrapping 341 method and significances by the permuation test 342 343 :param skip_zero: if True variables with estimated coefficient equal to 0 344 are omitted 345 :type skip_zero: boolean 346 """ 347 348 from string import join 349 labels = ('Variable', 'Coeff Est', 'Std Error', 'p') 350 lines = [join(['%10s' % l for l in labels], ' ')] 351 352 fmt = "%10s " + join(["%10.3f"]*3, " ") + " %5s" 353 fmt1 = "%10s %10.3f" 354 355 def get_star(p): 356 if p < 0.001: return "*"*3 357 elif p < 0.01: return "*"*2 358 elif p < 0.05: return "*" 359 elif p < 0.1: return "." 360 else: return " " 361 362 stars = get_star(self.p_vals[0]) 363 lines.append(fmt1 % ('Intercept', self.coef0)) 364 skipped = [] 365 for i in range(len(self.domain.attributes)): 366 if self.coefficients[i] == 0. and skip_zero: 367 skipped.append(self.domain.attributes[i].name) 368 continue 369 stars = get_star(self.p_vals[i]) 370 lines.append(fmt % (self.domain.attributes[i].name, 371 self.coefficients[i], self.std_errors_fixed_t[i], 372 self.p_vals[i], stars)) 373 lines.append("Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 empty 1") 374 lines.append("\n") 375 if skip_zero: 376 k = len(skipped) 377 if k == 0: 378 lines.append("All variables have nonzero regression coefficients. ") 379 else: 380 suff = "s" if k > 1 else "" 381 lines.append("For %d variable%s the regression coefficient equals 0: " \ 382 % (k, suff)) 383 for var in skipped: 384 lines.append(var) 385 return "\n".join(lines) 386 387 def __str__(self): 388 return self.to_string(skip_zero=True) 335 389 336 390 deprecated_members({"muX": "mu_x", … … 341 395 in_place=True)(LassoRegression) 342 396 343 344 @deprecated_keywords({"skipZero": "skip_zero"})345 def print_lasso_regression_model(lr, skip_zero=True):346 """Prettyprints Lasso regression model,347 i.e. estimated regression coefficients with standard errors348 and significances. Standard errors are obtained using bootstrapping349 method and significances by the permuation test350 351 :param lr: a Lasso regression model object.352 :type lr: :class:`LassoRegression`353 :param skip_zero: if True variables with estimated coefficient equal to 0354 are omitted355 :type skip_zero: boolean356 """357 358 from string import join359 m = lr360 labels = ('Variable', 'Coeff Est', 'Std Error', 'p')361 print join(['%10s' % l for l in labels], ' ')362 363 fmt = "%10s " + join(["%10.3f"]*3, " ") + " %5s"364 fmt1 = "%10s %10.3f"365 366 def get_star(p):367 if p < 0.001: return "*"*3368 elif p < 0.01: return "*"*2369 elif p < 0.05: return "*"370 elif p < 0.1: return "."371 else: return " "372 373 stars = get_star(m.p_vals[0])374 print fmt1 % ('Intercept', m.coef0)375 skipped = []376 for i in range(len(m.domain.attributes)):377 if m.coefficients[i] == 0. and skip_zero:378 skipped.append(m.domain.attributes[i].name)379 continue380 stars = get_star(m.p_vals[i])381 print fmt % (m.domain.attributes[i].name, \382 m.coefficients[i], m.std_errors_fixed_t[i], \383 m.p_vals[i], stars)384 print "Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 empty 1"385 print "\n"386 if skip_zero:387 k = len(skipped)388 if k == 0:389 print "All variables have nonzero regression coefficients. "390 else:391 suff = "s" if k > 1 else ""392 print "For %d variable%s the regression coefficient equals 0: " \393 % (k, suff)394 for var in skipped:395 print var396 397 398 397 if __name__ == "__main__": 399 398 … … 403 402 404 403 c = LassoRegressionLearner(table, t=len(table.domain)) 405 print _lasso_regression_model(c)404 print c
Note: See TracChangeset
for help on using the changeset viewer.