Changeset 10961:e38223aa3c8a in orange
Legend:
 Unmodified
 Added
 Removed

Orange/OrangeWidgets/Regression/OWLinearRegression.py
r10794 r10961 9 9 """ 10 10 11 import os,sys11 import sys 12 12 from OWWidget import * 13 13 … … 17 17 from Orange import feature as variable 18 18 19 19 20 class OWLinearRegression(OWWidget): 20 21 settingsList = ["name", "intercept", "use_ridge", "ridge_lambda", 21 "use_lasso", "t", "tol"] 22 23 def __init__(self, parent=None, signalManager=None, title="Linear Regression"): 24 OWWidget.__init__(self, parent, signalManager, title, wantMainArea=False) 25 22 "use_lasso", "lasso_lambda", "eps"] 23 24 def __init__(self, parent=None, signalManager=None, 25 title="Linear Regression"): 26 OWWidget.__init__(self, parent, signalManager, title, 27 wantMainArea=False) 28 26 29 self.inputs = [("Data", Orange.data.Table, self.set_data), 27 ("Preprocessor", PreprocessedLearner, self.set_preprocessor)] 28 29 self.outputs = [("Learner", Orange.core.Learner), 30 ("Preprocessor", PreprocessedLearner, 31 self.set_preprocessor)] 32 33 self.outputs = [("Learner", Orange.core.Learner), 30 34 ("Predictor", Orange.core.Classifier), 31 35 ("Model Statistics", Orange.data.Table)] 32 36 33 37 ########## 34 38 # Settings 35 39 ########## 36 40 37 41 self.name = "Linear Regression" 38 42 self.intercept = True … … 40 44 self.ridge_lambda = 1.0 41 45 self.use_lasso = False 42 self. t = 1.043 self. tol = 1e344 46 self.lasso_lambda = 0.1 47 self.eps = 1e6 48 45 49 self.loadSettings() 46 50 47 51 ##### 48 52 # GUI 49 53 ##### 50 51 OWGUI.lineEdit(self.controlArea, self, "name", box="Learner/predictor name", 54 55 OWGUI.lineEdit(self.controlArea, self, "name", 56 box="Learner/predictor name", 52 57 tooltip="Name of the learner/predictor") 53 58 54 59 OWGUI.checkBox(self.controlArea, self, 'intercept', 'Intercept') 55 56 bbox = OWGUI.radioButtonsInBox(self.controlArea, self, "use_lasso", [], 60 61 bbox = OWGUI.radioButtonsInBox(self.controlArea, self, "use_lasso", [], 57 62 box=None, 58 63 callback=self.on_method_changed 59 64 ) 65 60 66 rb = OWGUI.appendRadioButton(bbox, self, "use_lasso", 61 67 label="Ordinary/Ridge Linear Regression", … … 63 69 insertInto=bbox) 64 70 65 self.lm_box = box = OWGUI.indentedBox(self.controlArea, 66 sep=OWGUI.checkButtonOffsetHint(rb)) 71 self.lm_box = box = OWGUI.indentedBox( 72 self.controlArea, sep=OWGUI.checkButtonOffsetHint(rb) 73 ) 74 67 75 self.lm_box.setEnabled(not self.use_lasso) 68 76 69 77 OWGUI.doubleSpin(box, self, "ridge_lambda", 0.1, 100, step=0.1, 70 78 label="Ridge lambda", checked="use_ridge", … … 75 83 tooltip="", 76 84 insertInto=bbox) 77 78 self.lasso_box = box = OWGUI.indentedBox(self.controlArea, 79 sep=OWGUI.checkButtonOffsetHint(rb)) 80 self.lasso_box.setEnabled(self.use_lasso) 81 82 OWGUI.doubleSpin(box, self, "t", 0.0, 1e4, 0.1, 83 label="Lasso bound", 84 tooltip="Tuning parameter, upper bound for the L1norm of the regression coefficients", 85 86 self.lasso_box = box = OWGUI.indentedBox( 87 self.controlArea, sep=OWGUI.checkButtonOffsetHint(rb) 88 ) 89 90 self.lasso_box.setEnabled(self.use_lasso) 91 92 OWGUI.doubleSpin(box, self, "lasso_lambda", 0.0, 100.0, 1e2, 93 label="Lasso lambda", 85 94 ) 86 87 OWGUI.doubleSpin(box, self, " tol", 0.0, 1.0, 0.01,88 label="Tolerance", 89 tooltip=" Tolerance parameter, regression coefficients (absoulute value) under tolerance are set to 0",95 96 OWGUI.doubleSpin(box, self, "eps", 0.0, 0.01, 1e7, 97 label="Tolerance", 98 tooltip="Numerical tolerance." 90 99 ) 91 100 92 101 OWGUI.rubber(self.controlArea) 93 102 94 103 OWGUI.button(self.controlArea, self, "&Apply", 95 104 callback=self.apply, 96 tooltip="Send the learner on",105 tooltip="Send the learner/classifier on output", 97 106 autoDefault=True) 98 107 99 108 self.data = None 100 109 self.preprocessor = None 101 110 self.resize(300, 100) 102 111 self.apply() 103 112 104 113 def set_data(self, data=None): 105 114 if not self.isDataWithClass(data, Orange.core.VarTypes.Continuous, … … 107 116 data = None 108 117 self.data = data 109 118 110 119 def set_preprocessor(self, pproc=None): 111 120 self.preprocessor = pproc 112 121 113 122 def handleNewSignals(self): 114 123 self.apply() 115 124 116 125 def on_method_changed(self): 117 126 self.lm_box.setEnabled(not self.use_lasso) 118 127 self.lasso_box.setEnabled(self.use_lasso) 119 128 120 129 def apply(self): 121 130 if self.use_lasso: … … 123 132 else: 124 133 self.apply_ridge() 125 134 126 135 def apply_ridge(self): 127 136 if self.use_ridge: … … 134 143 if self.preprocessor: 135 144 learner = self.preprocessor.wrapLearner(learner) 136 145 137 146 self.error(0) 138 147 if self.data is not None: … … 142 151 except Exception, ex: 143 152 self.error(0, "An error during learning: %r" % ex) 144 153 145 154 self.send("Learner", learner) 146 155 self.send("Predictor", predictor) … … 148 157 149 158 def apply_lasso(self): 150 learner = lasso.LassoRegressionLearner(t=self.t, 151 tol=self.tol, n_boot=0, n_perm=0, 152 name=self.name) 159 learner = lasso.LassoRegressionLearner( 160 lasso_lambda=self.lasso_lambda, eps=self.eps, 161 n_boot=0, n_perm=0, 162 name=self.name 163 ) 164 153 165 predictor = None 154 166 155 167 if self.preprocessor is not None: 156 168 learner = self.preprocessor.wrapLearner(learner) … … 159 171 try: 160 172 if self.data is not None: 161 ll = lasso.LassoRegressionLearner(t=self.t, 162 tol=self.tol, n_boot=10, n_perm=10) 173 ll = lasso.LassoRegressionLearner( 174 lasso_lambda=self.lasso_lambda, eps=self.eps, 175 n_boot=10, n_perm=10 176 ) 163 177 predictor = ll(self.data) 164 178 predictor.name = self.name 165 179 except Exception, ex: 166 180 self.error(0, "An error during learning: %r" % ex) 167 181 168 182 self.send("Learner", learner) 169 183 self.send("Predictor", predictor) 170 184 self.send("Model Statistics", self.statistics_lasso(predictor)) 171 185 172 186 def statistics_olr(self, m): 173 187 if m is None: 174 188 return None 175 189 176 190 columns = [variable.String("Variable"), 177 191 variable.Continuous("Coeff Est"), 178 192 variable.Continuous("Std Error"), 179 193 variable.Continuous("tvalue"), 180 variable.Continuous("p") ,]181 194 variable.Continuous("p")] 195 182 196 domain = Orange.data.Domain(columns, None) 183 197 vars = ["Intercept"] if m.intercept else [] … … 185 199 stats = [] 186 200 geti = lambda list, i: list[i] if list is not None else "?" 187 201 188 202 for i, var in enumerate(vars): 189 203 coef = m.coefficients[i] … … 192 206 p = geti(m.p_vals, i) 193 207 stats.append([var, coef, std_err, t_val, p]) 194 208 195 209 return Orange.data.Table(domain, stats) 196 210 197 211 def statistics_lasso(self, m): 198 212 if m is None: 199 213 return None 200 214 201 215 columns = [variable.String("Variable"), 202 216 variable.Continuous("Coeff Est"), 203 217 variable.Continuous("Std Error"), 204 variable.Continuous("p") ,]205 218 variable.Continuous("p")] 219 206 220 domain = Orange.data.Domain(columns, None) 207 221 vars = [] … … 209 223 stats = [["Intercept", m.coef0, "?", "?"]] 210 224 geti = lambda list, i: list[i] if list is not None else "?" 211 225 212 226 for i, var in enumerate(vars): 213 227 coef = m.coefficients[i] 214 std_err = geti(m.std_errors _fixed_t, i)228 std_err = geti(m.std_errors, i) 215 229 p = geti(m.p_vals, i) 216 230 stats.append([var, coef, std_err, p]) 217 231 218 232 return Orange.data.Table(domain, stats) 219 220 233 234 221 235 if __name__ == "__main__": 222 236 app = QApplication(sys.argv)
Note: See TracChangeset
for help on using the changeset viewer.