Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 20 additions & 31 deletions Orange/widgets/regression/owlinearregression.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class OWLinearRegression(OWBaseLearner):
ridge = settings.Setting(False)
reg_type = settings.Setting(OLS)
alpha_index = settings.Setting(0)
l1_ratio = settings.Setting(0.5)
l2_ratio = settings.Setting(0.5)
autosend = settings.Setting(True)

want_main_area = False
Expand Down Expand Up @@ -65,55 +65,44 @@ def add_main_layout(self):
gui.widgetLabel(box4, "Elastic net mixing:")
box5 = gui.hBox(box4)
gui.widgetLabel(box5, "L1")
self.l1_ratio_slider = gui.hSlider(
box5, self, "l1_ratio", minValue=0.01, maxValue=0.99,
self.l2_ratio_slider = gui.hSlider(
box5, self, "l2_ratio", minValue=0.01, maxValue=0.99,
intOnly=False, ticks=0.1, createLabel=False, width=120,
step=0.01, callback=self._l1_ratio_changed)
step=0.01, callback=self._l2_ratio_changed)
gui.widgetLabel(box5, "L2")
self.l1_ratio_label = gui.widgetLabel(
self.l2_ratio_label = gui.widgetLabel(
box4, "",
sizePolicy=(QSizePolicy.MinimumExpanding, QSizePolicy.Fixed))
self.l1_ratio_label.setAlignment(Qt.AlignCenter)
self.l2_ratio_label.setAlignment(Qt.AlignCenter)

def add_bottom_buttons(self):
box5 = gui.hBox(self.controlArea)
box5.layout().setAlignment(Qt.AlignCenter)
self._set_l1_ratio_label()

auto_commit = gui.auto_commit(
self.controlArea, self, "autosend", "Apply")
auto_commit.layout().insertWidget(0, self.report_button)
auto_commit.layout().insertSpacing(1, 20)
self.report_button.setMinimumWidth(150)

self._set_l2_ratio_label()
self.layout().setSizeConstraint(QLayout.SetFixedSize)
self.alpha_slider.setEnabled(self.reg_type != self.OLS)
self.l1_ratio_slider.setEnabled(self.reg_type == self.Elastic)
self.l2_ratio_slider.setEnabled(self.reg_type == self.Elastic)

def handleNewSignals(self):
self.commit()
self.apply()

def _reg_type_changed(self):
self.alpha_slider.setEnabled(self.reg_type != self.OLS)
self.l1_ratio_slider.setEnabled(self.reg_type == self.Elastic)
self.commit()
self.l2_ratio_slider.setEnabled(self.reg_type == self.Elastic)
self.apply()

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

def _alpha_changed(self):
self._set_alpha_label()
self.commit()

def _set_l1_ratio_label(self):
self.l1_ratio_label.setText(
"{:.{}f} : {:.{}f}".format(self.l1_ratio, 2, 1 - self.l1_ratio, 2))
self.apply()

def _l1_ratio_changed(self):
self._set_l1_ratio_label()
self.commit()
def _set_l2_ratio_label(self):
self.l2_ratio_label.setText(
"{:.{}f} : {:.{}f}".format(1 - self.l2_ratio, 2, self.l2_ratio, 2))

def commit(self):
def _l2_ratio_changed(self):
self._set_l2_ratio_label()
self.apply()

def create_learner(self):
Expand All @@ -128,7 +117,7 @@ def create_learner(self):
learner = LassoRegressionLearner(alpha=alpha, **args)
elif self.reg_type == OWLinearRegression.Elastic:
learner = ElasticNetLearner(alpha=alpha,
l1_ratio=self.l1_ratio, **args)
l1_ratio=1 - self.l2_ratio, **args)
return learner

def update_model(self):
Expand Down Expand Up @@ -157,8 +146,8 @@ def get_learner_parameters(self):
regularization = ("Elastic Net Regression with α={}"
" and L1:L2 ratio of {}:{}"
.format(self.alphas[self.alpha_index],
self.l1_ratio,
1 - self.l1_ratio))
self.l2_ratio,
1 - self.l2_ratio))
return ("Regularization", regularization),


Expand Down
9 changes: 9 additions & 0 deletions Orange/widgets/regression/tests/test_owlinearregression.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from Orange.widgets.regression.owlinearregression import OWLinearRegression
from Orange.widgets.tests.base import WidgetTest, WidgetLearnerTestMixin


class TestOWLinearRegression(WidgetTest, WidgetLearnerTestMixin):
def setUp(self):
self.widget = self.create_widget(OWLinearRegression,
stored_settings={"auto_apply": False})
self.init()
3 changes: 3 additions & 0 deletions Orange/widgets/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,9 @@ def init(self):
self.model_class = ModelRegression
self.parameters = []

def test_has_unconditional_apply(self):
self.assertTrue(hasattr(self.widget, "unconditional_apply"))

def test_input_data(self):
"""Check widget's data with data on the input"""
self.assertEqual(self.widget.data, None)
Expand Down