Skip to content

Commit 809e955

Browse files
authored
Merge pull request #1601 from janezd/fix-lin-reg
[FIX] Linear regression: Fix Elastic net; Fix Auto-apply buttons
2 parents 33174e6 + 0a22480 commit 809e955

File tree

3 files changed

+32
-31
lines changed

3 files changed

+32
-31
lines changed

Orange/widgets/regression/owlinearregression.py

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class OWLinearRegression(OWBaseLearner):
2929
ridge = settings.Setting(False)
3030
reg_type = settings.Setting(OLS)
3131
alpha_index = settings.Setting(0)
32-
l1_ratio = settings.Setting(0.5)
32+
l2_ratio = settings.Setting(0.5)
3333
autosend = settings.Setting(True)
3434

3535
want_main_area = False
@@ -65,55 +65,44 @@ def add_main_layout(self):
6565
gui.widgetLabel(box4, "Elastic net mixing:")
6666
box5 = gui.hBox(box4)
6767
gui.widgetLabel(box5, "L1")
68-
self.l1_ratio_slider = gui.hSlider(
69-
box5, self, "l1_ratio", minValue=0.01, maxValue=0.99,
68+
self.l2_ratio_slider = gui.hSlider(
69+
box5, self, "l2_ratio", minValue=0.01, maxValue=0.99,
7070
intOnly=False, ticks=0.1, createLabel=False, width=120,
71-
step=0.01, callback=self._l1_ratio_changed)
71+
step=0.01, callback=self._l2_ratio_changed)
7272
gui.widgetLabel(box5, "L2")
73-
self.l1_ratio_label = gui.widgetLabel(
73+
self.l2_ratio_label = gui.widgetLabel(
7474
box4, "",
7575
sizePolicy=(QSizePolicy.MinimumExpanding, QSizePolicy.Fixed))
76-
self.l1_ratio_label.setAlignment(Qt.AlignCenter)
76+
self.l2_ratio_label.setAlignment(Qt.AlignCenter)
7777

78-
def add_bottom_buttons(self):
7978
box5 = gui.hBox(self.controlArea)
8079
box5.layout().setAlignment(Qt.AlignCenter)
81-
self._set_l1_ratio_label()
82-
83-
auto_commit = gui.auto_commit(
84-
self.controlArea, self, "autosend", "Apply")
85-
auto_commit.layout().insertWidget(0, self.report_button)
86-
auto_commit.layout().insertSpacing(1, 20)
87-
self.report_button.setMinimumWidth(150)
88-
80+
self._set_l2_ratio_label()
8981
self.layout().setSizeConstraint(QLayout.SetFixedSize)
9082
self.alpha_slider.setEnabled(self.reg_type != self.OLS)
91-
self.l1_ratio_slider.setEnabled(self.reg_type == self.Elastic)
83+
self.l2_ratio_slider.setEnabled(self.reg_type == self.Elastic)
9284

9385
def handleNewSignals(self):
94-
self.commit()
86+
self.apply()
9587

9688
def _reg_type_changed(self):
9789
self.alpha_slider.setEnabled(self.reg_type != self.OLS)
98-
self.l1_ratio_slider.setEnabled(self.reg_type == self.Elastic)
99-
self.commit()
90+
self.l2_ratio_slider.setEnabled(self.reg_type == self.Elastic)
91+
self.apply()
10092

10193
def _set_alpha_label(self):
10294
self.alpha_label.setText("Alpha: {}".format(self.alphas[self.alpha_index]))
10395

10496
def _alpha_changed(self):
10597
self._set_alpha_label()
106-
self.commit()
107-
108-
def _set_l1_ratio_label(self):
109-
self.l1_ratio_label.setText(
110-
"{:.{}f} : {:.{}f}".format(self.l1_ratio, 2, 1 - self.l1_ratio, 2))
98+
self.apply()
11199

112-
def _l1_ratio_changed(self):
113-
self._set_l1_ratio_label()
114-
self.commit()
100+
def _set_l2_ratio_label(self):
101+
self.l2_ratio_label.setText(
102+
"{:.{}f} : {:.{}f}".format(1 - self.l2_ratio, 2, self.l2_ratio, 2))
115103

116-
def commit(self):
104+
def _l2_ratio_changed(self):
105+
self._set_l2_ratio_label()
117106
self.apply()
118107

119108
def create_learner(self):
@@ -128,7 +117,7 @@ def create_learner(self):
128117
learner = LassoRegressionLearner(alpha=alpha, **args)
129118
elif self.reg_type == OWLinearRegression.Elastic:
130119
learner = ElasticNetLearner(alpha=alpha,
131-
l1_ratio=self.l1_ratio, **args)
120+
l1_ratio=1 - self.l2_ratio, **args)
132121
return learner
133122

134123
def update_model(self):
@@ -157,8 +146,8 @@ def get_learner_parameters(self):
157146
regularization = ("Elastic Net Regression with α={}"
158147
" and L1:L2 ratio of {}:{}"
159148
.format(self.alphas[self.alpha_index],
160-
self.l1_ratio,
161-
1 - self.l1_ratio))
149+
self.l2_ratio,
150+
1 - self.l2_ratio))
162151
return ("Regularization", regularization),
163152

164153

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from Orange.widgets.regression.owlinearregression import OWLinearRegression
2+
from Orange.widgets.tests.base import WidgetTest, WidgetLearnerTestMixin
3+
4+
5+
class TestOWLinearRegression(WidgetTest, WidgetLearnerTestMixin):
6+
def setUp(self):
7+
self.widget = self.create_widget(OWLinearRegression,
8+
stored_settings={"auto_apply": False})
9+
self.init()

Orange/widgets/tests/base.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,9 @@ def init(self):
331331
self.model_class = ModelRegression
332332
self.parameters = []
333333

334+
def test_has_unconditional_apply(self):
335+
self.assertTrue(hasattr(self.widget, "unconditional_apply"))
336+
334337
def test_input_data(self):
335338
"""Check widget's data with data on the input"""
336339
self.assertEqual(self.widget.data, None)

0 commit comments

Comments
 (0)