Changeset 10777:546aa4c30fdc in orange
 Timestamp:
 04/10/12 16:21:31 (2 years ago)
 Branch:
 default
 Location:
 Orange
 Files:

 2 edited
Legend:
 Unmodified
 Added
 Removed

Orange/regression/linear.py
r10643 r10777 9 9 10 10 11 `Linear regression <http://en.wikipedia.org/wiki/Linear_regression>`_ is a statistical regression method 12 which tries to predict a value of a continuous response (class) variable based on the values of several predictors. 13 The model assumes that the response variable is a linear combination of the predictors, the task of linear regression 14 is therefore to fit the unknown coefficients. 11 `Linear regression <http://en.wikipedia.org/wiki/Linear_regression>`_ 12 is a statistical regression method which tries to predict a value of 13 a continuous response (class) variable based on the values of several 14 predictors. The model assumes that the response variable is a linear 15 combination of the predictors, the task of linear regression is 16 therefore to fit the unknown coefficients. 15 17 16 18 To fit the regression parameters on housing data set use the following code: … … 49 51 :: 50 52 51 Actual: 24.00 0, predicted: 30.00452 Actual: 21.60 0, predicted: 25.02653 Actual: 34.70 0, predicted: 30.56854 Actual: 33.40 0, predicted: 28.60755 Actual: 36.20 0, predicted: 27.94453 Actual: 24.00, predicted: 30.00 54 Actual: 21.60, predicted: 25.03 55 Actual: 34.70, predicted: 30.57 56 Actual: 33.40, predicted: 28.61 57 Actual: 36.20, predicted: 27.94 56 58 57 59 ========================= … … 70 72 :: 71 73 72 Variable Coeff Est Std Error tvalue p74 Variable Coeff Est Std Error tvalue p 73 75 Intercept 36.459 5.103 7.144 0.000 *** 74 76 CRIM 0.108 0.033 3.287 0.001 ** … … 85 87 B 0.009 0.003 3.467 0.001 *** 86 88 LSTAT 0.525 0.051 10.347 0.000 *** 87 Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 empty 188 89 89 90 90 … … 93 93 =================== 94 94 95 To use stepwise regression initialize learner with stepwise=True.96 The upper and lower bound for significance are cont olled with97 add_sig and remove_sig.95 To use stepwise regression initialize learner with ``stepwise=True``. 96 The upper and lower bound for significance are controlled with 97 ``add_sig`` and ``remove_sig``. 98 98 99 99 .. literalinclude:: code/linearexample.py 100 100 :lines: 2023,25 101 101 102 As you can see from the output the nonsignificant coefficients103 have been removed from the output102 As you can see from the output, the nonsignificant coefficients 103 have been removed from the model. 104 104 105 105 :: … … 119 119 TAX 0.012 0.003 3.493 0.001 *** 120 120 Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 empty 1 121 122 123 121 124 122 """ … … 467 465 468 466 """ 469 from string import join470 labels = ('Variable', 'Coeff Est', 'Std Error', 'tvalue', 'p')471 lines = [join(['%10s' % l for l in labels], ' ')]472 473 fmt = "%10s " + join(["%10.3f"] * 4, " ") + " %5s"474 if not self.p_vals:475 raise ValueError("Model does not contain model statistics.")476 467 def get_star(p): 477 468 if p < 0.001: return "*" * 3 … … 481 472 else: return " " 482 473 483 if self.intercept == True: 484 stars = get_star(self.p_vals[0]) 485 lines.append(fmt % ('Intercept', self.coefficients[0], 486 self.std_error[0], self.t_scores[0], 487 self.p_vals[0], stars)) 488 for i in range(len(self.domain.attributes)): 489 stars = get_star(self.p_vals[i + 1]) 490 lines.append(fmt % (self.domain.attributes[i].name, 491 self.coefficients[i + 1], self.std_error[i + 1], 492 self.t_scores[i + 1], self.p_vals[i + 1], stars)) 474 labels = ("Variable", "Coeff Est", "Std Error", "tvalue", "p", "") 475 names = [a.name for a in self.domain.attributes] 476 477 if self.intercept: 478 names = ["Intercept"] + names 479 480 float_fmt = "%10.3f" 481 float_str = float_fmt.__mod__ 482 483 coefs = map(float_str, self.coefficients) 484 if self.std_error is not None: 485 std_error = map(float_str, self.std_error) 493 486 else: 494 for i in range(len(self.domain.attributes)): 495 stars = get_star(self.p_vals[i]) 496 lines.append(fmt % (self.domain.attributes[i].name, 497 self.coefficients[i], self.std_error[i], 498 self.t_scores[i], self.p_vals[i], stars)) 499 lines.append("Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 empty 1") 487 std_error = None 488 489 if self.t_scores is not None: 490 t_scores = map(float_str, self.t_scores) 491 else: 492 t_scores = None 493 494 if self.p_vals is not None: 495 p_vals = map(float_str, self.p_vals) 496 stars = [get_star(p) for p in self.p_vals] 497 else: 498 p_vals = None 499 stars = None 500 501 columns = [names, coefs, std_error, t_scores, p_vals, stars] 502 labels = [label for label, c in zip(labels, columns) if c is not None] 503 columns = [c for c in columns if c is not None] 504 name_len = max([len(name) for name in names] + [10]) 505 fmt_name = "%%%is" % name_len 506 lines = [" ".join([fmt_name % labels[0]] + \ 507 ["%10s" % l for l in labels[1:]]) 508 ] 509 510 if p_vals is not None: 511 fmt = fmt_name + " " + " ".join(["%10s"] * (len(labels)  2)) + " %5s" 512 else: 513 fmt = fmt_name + " " + " ".join(["%10s"] * (len(labels)  1)) 514 515 for i in range(len(names)): 516 lines.append(fmt % tuple([c[i] for c in columns])) 517 518 if self.p_vals is not None: 519 lines.append("Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 empty 1") 500 520 return "\n".join(lines) 501 521 … … 599 619 c = LinearRegressionLearner(table) 600 620 print c 601 
Orange/testing/unit/tests/test_linregression.py
r10655 r10777 28 28 def test_learner_on(self, dataset): 29 29 testing.LearnerTestCase.test_learner_on(self, dataset) 30 if self.classifier.p_vals: 31 self.assertTrue(isinstance(self.classifier.to_string(), str)) 30 self.assertTrue(isinstance(self.classifier.to_string(), str)) 32 31 33 32
Note: See TracChangeset
for help on using the changeset viewer.