Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENH] Add an option to set intercept in linear regression to 0 #4958

Merged
merged 3 commits into from
Sep 18, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 9 additions & 1 deletion Orange/regression/linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ def score(self, data):
class LinearRegressionLearner(SklLearner, _FeatureScorerMixin):
__wraps__ = skl_linear_model.LinearRegression

def __init__(self, preprocessors=None):
# Arguments are needed for signatures, pylint: disable=unused-argument
def __init__(self, preprocessors=None, fit_intercept=True):
super().__init__(preprocessors=preprocessors)
self.params = vars()

def fit(self, X, Y, W=None):
model = super().fit(X, Y, W)
Expand All @@ -39,6 +41,7 @@ def fit(self, X, Y, W=None):
class RidgeRegressionLearner(LinearRegressionLearner):
__wraps__ = skl_linear_model.Ridge

# Arguments are needed for signatures, pylint: disable=unused-argument
def __init__(self, alpha=1.0, fit_intercept=True,
normalize=False, copy_X=True, max_iter=None,
tol=0.001, solver='auto', preprocessors=None):
Expand All @@ -49,6 +52,7 @@ def __init__(self, alpha=1.0, fit_intercept=True,
class LassoRegressionLearner(LinearRegressionLearner):
__wraps__ = skl_linear_model.Lasso

# Arguments are needed for signatures, pylint: disable=unused-argument
def __init__(self, alpha=1.0, fit_intercept=True, normalize=False,
precompute=False, copy_X=True, max_iter=1000,
tol=0.0001, warm_start=False, positive=False,
Expand All @@ -60,6 +64,7 @@ def __init__(self, alpha=1.0, fit_intercept=True, normalize=False,
class ElasticNetLearner(LinearRegressionLearner):
__wraps__ = skl_linear_model.ElasticNet

# Arguments are needed for signatures, pylint: disable=unused-argument
def __init__(self, alpha=1.0, l1_ratio=0.5, fit_intercept=True,
normalize=False, precompute=False, max_iter=1000,
copy_X=True, tol=0.0001, warm_start=False, positive=False,
Expand All @@ -71,6 +76,7 @@ def __init__(self, alpha=1.0, l1_ratio=0.5, fit_intercept=True,
class ElasticNetCVLearner(LinearRegressionLearner):
__wraps__ = skl_linear_model.ElasticNetCV

# Arguments are needed for signatures, pylint: disable=unused-argument
def __init__(self, l1_ratio=0.5, eps=0.001, n_alphas=100, alphas=None,
fit_intercept=True, normalize=False, precompute='auto',
max_iter=1000, tol=0.0001, cv=5, copy_X=True,
Expand All @@ -83,6 +89,7 @@ class SGDRegressionLearner(LinearRegressionLearner):
__wraps__ = skl_linear_model.SGDRegressor
preprocessors = SklLearner.preprocessors + [Normalize()]

# Arguments are needed for signatures, pylint: disable=unused-argument
def __init__(self, loss='squared_loss', penalty='l2', alpha=0.0001,
l1_ratio=0.15, fit_intercept=True, max_iter=5, tol=1e-3,
shuffle=True, epsilon=0.1, n_jobs=1, random_state=None,
Expand Down Expand Up @@ -140,6 +147,7 @@ def __str__(self):

class PolynomialModel(Model):
def __init__(self, model, polyfeatures):
super().__init__()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is suggested by lint but otherwise unrelated to the issue.

self.model = model
self.polyfeatures = polyfeatures

Expand Down
25 changes: 20 additions & 5 deletions Orange/widgets/model/owlinearregression.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class Outputs(OWBaseLearner.Outputs):
reg_type = settings.Setting(OLS)
alpha_index = settings.Setting(0)
l2_ratio = settings.Setting(0.5)
fit_intercept = settings.Setting(True)
autosend = settings.Setting(True)

alphas = list(chain([x / 10000 for x in range(1, 10)],
Expand All @@ -51,6 +52,11 @@ class Outputs(OWBaseLearner.Outputs):

def add_main_layout(self):
# this is part of init, pylint: disable=attribute-defined-outside-init
box = gui.hBox(self.controlArea, "Parameters")
gui.checkBox(box, self, "fit_intercept",
"Fit intercept (unchecking it fixes it to zero)",
callback=self._intercept_changed)

box = gui.hBox(self.controlArea, "Regularization")
gui.radioButtons(box, self, "reg_type",
btnLabels=self.REGULARIZATION_TYPES,
Expand Down Expand Up @@ -93,6 +99,9 @@ def add_main_layout(self):
def handleNewSignals(self):
self.apply()

def _intercept_changed(self):
self.apply()

def _reg_type_changed(self):
self.controls.alpha_index.setEnabled(self.reg_type != self.OLS)
self.l2_ratio_slider.setEnabled(self.reg_type == self.Elastic)
Expand All @@ -116,7 +125,8 @@ def _l2_ratio_changed(self):
def create_learner(self):
alpha = self.alphas[self.alpha_index]
preprocessors = self.preprocessors
args = {"preprocessors": preprocessors}
args = dict(preprocessors=preprocessors,
fit_intercept=self.fit_intercept)
if self.reg_type == OWLinearRegression.OLS:
learner = LinearRegressionLearner(**args)
elif self.reg_type == OWLinearRegression.Ridge:
Expand All @@ -134,9 +144,11 @@ def update_model(self):
if self.model is not None:
domain = Domain(
[ContinuousVariable("coef")], metas=[StringVariable("name")])
coefs = [self.model.intercept] + list(self.model.coefficients)
names = ["intercept"] + \
[attr.name for attr in self.model.domain.attributes]
coefs = list(self.model.coefficients)
names = [attr.name for attr in self.model.domain.attributes]
if self.fit_intercept:
coefs.insert(0, self.model.intercept)
names.insert(0, "intercept")
coef_table = Table.from_list(domain, list(zip(coefs, names)))
coef_table.name = "coefficients"
self.Outputs.coefficients.send(coef_table)
Expand All @@ -155,7 +167,10 @@ def get_learner_parameters(self):
.format(self.alphas[self.alpha_index],
self.l2_ratio,
1 - self.l2_ratio))
return (("Regularization", regularization), )
return (
("Regularization", regularization),
("Fit intercept", ["No", "Yes"][self.fit_intercept])
)


if __name__ == "__main__": # pragma: no cover
Expand Down