Changeset 9245:9d1452affd3e in orange
 Timestamp:
 11/22/11 18:16:08 (2 years ago)
 Branch:
 default
 Convert:
 d2a815cf62206a94ac828111bf5fc27573dcb42b
 Location:
 orange/OrangeWidgets/Regression
 Files:

 1 deleted
 1 edited
Legend:
 Unmodified
 Added
 Removed

orange/OrangeWidgets/Regression/OWLinearRegression.py
r9223 r9245 5 5 <priority>10</priority> 6 6 <category>Regression</category> 7 <keywords>linear, model </keywords>7 <keywords>linear, model, ridge, regression, lasso, least, absolute, shrinkage</keywords> 8 8 9 9 """ … … 13 13 14 14 import Orange 15 from Orange.regression import linear 15 from Orange.regression import linear, lasso 16 16 from orngWrap import PreprocessedLearner 17 from Orange.data import variable 17 18 18 19 class OWLinearRegression(OWWidget): 19 settingsList = ["name", "use_ridge", "ridge_lambda"] 20 settingsList = ["name", "use_ridge", "ridge_lambda", 21 "use_lasso", "t", "tol"] 20 22 21 23 def __init__(self, parent=None, signalManager=None, title="Linear Regression"): 22 24 OWWidget.__init__(self, parent, signalManager, title, wantMainArea=False) 23 25 24 self.inputs = [("Training data", Orange.data.Table, self.set_data), ("Preprocessor", PreprocessedLearner, self.set_preprocessor)] 25 self.outputs = [("Learner", Orange.core.Learner), ("Predictor", Orange.core.Classifier)] 26 self.inputs = [("Training data", Orange.data.Table, self.set_data), 27 ("Preprocessor", PreprocessedLearner, self.set_preprocessor)] 28 29 self.outputs = [("Learner", Orange.core.Learner), 30 ("Predictor", Orange.core.Classifier), 31 ("Model Statistics", Orange.data.Table)] 26 32 27 33 ########## … … 32 38 self.use_ridge = False 33 39 self.ridge_lambda = 1.0 40 self.use_lasso = False 41 self.t = 1.0 42 self.tol = 1e3 43 34 44 self.loadSettings() 35 45 … … 38 48 ##### 39 49 40 OWGUI.lineEdit(self.controlArea, self, "name", box="Learner/predictor name", 50 OWGUI.lineEdit(self.controlArea, self, "name", box="Learner/predictor name", 41 51 tooltip="Name of the learner/predictor") 42 43 OWGUI.checkWithSpin(self.controlArea, self, "Ridge lambda", 1, 10, 44 "use_ridge", "ridge_lambda", step=1, 52 53 bbox = OWGUI.radioButtonsInBox(self.controlArea, self, "use_lasso", [], 54 box=None, 55 callback=self.on_method_changed 56 ) 57 rb = OWGUI.appendRadioButton(bbox, self, "use_lasso", 58 label="Ordinary/Ridge Linear Regression", 59 tooltip="", 60 insertInto=bbox) 61 62 self.lm_box = box = OWGUI.indentedBox(self.controlArea, 63 sep=OWGUI.checkButtonOffsetHint(rb)) 64 self.lm_box.setEnabled(not self.use_lasso) 65 66 OWGUI.checkWithSpin(box, self, "Ridge lambda", 1, 10, 67 "use_ridge", "ridge_lambda", step=1, 45 68 tooltip="Ridge lambda for ridge regression") 69 70 rb = OWGUI.appendRadioButton(bbox, self, "use_lasso", 71 label="LASSO Regression", 72 tooltip="", 73 insertInto=bbox) 74 75 self.lasso_box = box = OWGUI.indentedBox(self.controlArea, 76 sep=OWGUI.checkButtonOffsetHint(rb)) 77 self.lasso_box.setEnabled(self.use_lasso) 78 79 OWGUI.doubleSpin(box, self, "t", 0.0, 1e4, 0.1, 80 label="Lasso bound", 81 tooltip="Tuning parameter, upper bound for the L1norm of the regression coefficients", 82 ) 83 84 OWGUI.doubleSpin(box, self, "tol", 0.0, 1.0, 0.01, 85 label="Tolerance", 86 tooltip="Tolerance parameter, regression coefficients (absoulute value) under tolerance are set to 0", 87 ) 46 88 47 89 OWGUI.button(self.controlArea, self, "&Apply", … … 64 106 self.apply() 65 107 108 def on_method_changed(self): 109 self.lm_box.setEnabled(not self.use_lasso) 110 self.lasso_box.setEnabled(self.use_lasso) 111 66 112 def apply(self): 113 if self.use_lasso: 114 self.apply_lasso() 115 else: 116 self.apply_ridge() 117 118 def apply_ridge(self): 67 119 if self.use_ridge: 68 120 learner = linear.LinearRegressionLearner(name=self.name, … … 84 136 self.send("Learner", learner) 85 137 self.send("Predictor", predictor) 86 87 138 self.send("Model Statistics", self.statistics_olr(predictor)) 139 140 def apply_lasso(self): 141 learner = lasso.LassoRegressionLearner(t=self.t, 142 tol=self.tol, n_boot=0, n_perm=0, 143 name=self.name) 144 predictor = None 145 146 if self.preprocessor is not None: 147 learner = self.preprocessor.wrapLearner(learner) 148 149 self.error(0) 150 try: 151 if self.data is not None: 152 ll = lasso.LassoRegressionLearner(t=self.t, 153 tol=self.tol, n_boot=10, n_perm=10) 154 predictor = ll(self.data) 155 predictor.name = self.name 156 except Exception, ex: 157 self.error(0, "An error during learning: %r" % ex) 158 159 self.send("Learner", learner) 160 self.send("Predictor", predictor) 161 self.send("Model Statistics", self.statistics_lasso(predictor)) 162 163 def statistics_olr(self, m): 164 if m is None: 165 return None 166 167 columns = [variable.String("Variable"), 168 variable.Continuous("Coeff Est"), 169 variable.Continuous("Std Error"), 170 variable.Continuous("tvalue"), 171 variable.Continuous("p"),] 172 173 domain = Orange.data.Domain(columns, None) 174 vars = ["Intercept"] #if m.intercept else [] 175 vars.extend([a.name for a in m.domain.attributes]) 176 stats = [] 177 geti = lambda list, i: list[i] if list is not None else "?" 178 179 for i, var in enumerate(vars): 180 coef = m.coefficients[i] 181 std_err = geti(m.std_error, i) 182 t_val = geti(m.t_scores, i) 183 p = geti(m.p_vals, i) 184 stats.append([var, coef, std_err, t_val, p]) 185 186 print Orange.data.Table(domain, stats)[:] 187 return Orange.data.Table(domain, stats) 188 189 def statistics_lasso(self, m): 190 if m is None: 191 return None 192 193 columns = [variable.String("Variable"), 194 variable.Continuous("Coeff Est"), 195 variable.Continuous("Std Error"), 196 variable.Continuous("p"),] 197 198 domain = Orange.data.Domain(columns, None) 199 vars = [] 200 vars.extend([a.name for a in m.domain.attributes]) 201 stats = [["Intercept", m.coef0, "?", "?"]] 202 geti = lambda list, i: list[i] if list is not None else "?" 203 204 for i, var in enumerate(vars): 205 coef = m.coefficients[i] 206 std_err = geti(m.std_errors_fixed_t, i) 207 p = geti(m.p_vals, i) 208 stats.append([var, coef, std_err, p]) 209 210 return Orange.data.Table(domain, stats) 211 212 88 213 if __name__ == "__main__": 89 214 app = QApplication(sys.argv) 90 215 w = OWLinearRegression() 91 w.set_data(Orange.data.Table(" autompg"))216 w.set_data(Orange.data.Table("housing")) 92 217 w.show() 93 218 app.exec_() 94 # w.saveSettings() 95 219 # w.saveSettings()
Note: See TracChangeset
for help on using the changeset viewer.