Skip to content

Commit

Permalink
Remove parameter normalize in linear models (#686)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Geekman committed May 12, 2022
1 parent 672a60a commit f898aea
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 41 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
-
-
-
-
- Remove parameter normalize in linear models ([#686](https://github.com/tinkoff-ai/etna/pull/686))
-
-

Expand Down
40 changes: 6 additions & 34 deletions etna/models/linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class LinearPerSegmentModel(SklearnPerSegmentModel):
"""Class holding per segment :py:class:`sklearn.linear_model.LinearRegression`."""

def __init__(self, fit_intercept: bool = True, normalize: bool = False, **kwargs):
def __init__(self, fit_intercept: bool = True, **kwargs):
"""
Create instance of LinearModel with given parameters.
Expand All @@ -17,25 +17,16 @@ def __init__(self, fit_intercept: bool = True, normalize: bool = False, **kwargs
fit_intercept:
Whether to calculate the intercept for this model. If set to False, no intercept will be used in
calculations (i.e. data is expected to be centered).
normalize:
This parameter is ignored when ``fit_intercept`` is set to False.
If True, the regressors X will be normalized before regression
by subtracting the mean and dividing by the l2-norm.
"""
self.fit_intercept = fit_intercept
self.normalize = normalize
self.kwargs = kwargs
super().__init__(
regressor=LinearRegression(fit_intercept=self.fit_intercept, normalize=self.normalize, **self.kwargs)
)
super().__init__(regressor=LinearRegression(fit_intercept=self.fit_intercept, **self.kwargs))


class ElasticPerSegmentModel(SklearnPerSegmentModel):
"""Class holding per segment :py:class:`sklearn.linear_model.ElasticNet`."""

def __init__(
self, alpha: float = 1.0, l1_ratio: float = 0.5, fit_intercept: bool = True, normalize: bool = False, **kwargs
):
def __init__(self, alpha: float = 1.0, l1_ratio: float = 0.5, fit_intercept: bool = True, **kwargs):
"""
Create instance of ElasticNet with given parameters.
Expand All @@ -58,21 +49,16 @@ def __init__(
fit_intercept:
Whether to calculate the intercept for this model. If set to False, no intercept will be used in
calculations (i.e. data is expected to be centered).
normalize:
This parameter is ignored when fit_intercept is set to False. If True, the regressors X will be normalized
before regression by subtracting the mean and dividing by the l2-norm.
"""
self.alpha = alpha
self.l1_ratio = l1_ratio
self.fit_intercept = fit_intercept
self.normalize = normalize
self.kwargs = kwargs
super().__init__(
regressor=ElasticNet(
alpha=self.alpha,
l1_ratio=self.l1_ratio,
fit_intercept=self.fit_intercept,
normalize=self.normalize,
**self.kwargs,
)
)
Expand All @@ -81,7 +67,7 @@ def __init__(
class LinearMultiSegmentModel(SklearnMultiSegmentModel):
"""Class holding :py:class:`sklearn.linear_model.LinearRegression` for all segments."""

def __init__(self, fit_intercept: bool = True, normalize: bool = False, **kwargs):
def __init__(self, fit_intercept: bool = True, **kwargs):
"""
Create instance of LinearModel with given parameters.
Expand All @@ -90,25 +76,16 @@ def __init__(self, fit_intercept: bool = True, normalize: bool = False, **kwargs
fit_intercept:
Whether to calculate the intercept for this model. If set to False, no intercept will be used in
calculations (i.e. data is expected to be centered).
normalize:
This parameter is ignored when ``fit_intercept`` is set to False.
If True, the regressors X will be normalized before regression
by subtracting the mean and dividing by the l2-norm.
"""
self.fit_intercept = fit_intercept
self.normalize = normalize
self.kwargs = kwargs
super().__init__(
regressor=LinearRegression(fit_intercept=self.fit_intercept, normalize=self.normalize, **self.kwargs)
)
super().__init__(regressor=LinearRegression(fit_intercept=self.fit_intercept, **self.kwargs))


class ElasticMultiSegmentModel(SklearnMultiSegmentModel):
"""Class holding :py:class:`sklearn.linear_model.ElasticNet` for all segments."""

def __init__(
self, alpha: float = 1.0, l1_ratio: float = 0.5, fit_intercept: bool = True, normalize: bool = False, **kwargs
):
def __init__(self, alpha: float = 1.0, l1_ratio: float = 0.5, fit_intercept: bool = True, **kwargs):
"""
Create instance of ElasticNet with given parameters.
Expand All @@ -131,21 +108,16 @@ def __init__(
fit_intercept:
Whether to calculate the intercept for this model. If set to False, no intercept will be used in
calculations (i.e. data is expected to be centered).
normalize:
This parameter is ignored when fit_intercept is set to False. If True, the regressors X will be normalized
before regression by subtracting the mean and dividing by the l2-norm.
"""
self.alpha = alpha
self.l1_ratio = l1_ratio
self.fit_intercept = fit_intercept
self.normalize = normalize
self.kwargs = kwargs
super().__init__(
regressor=ElasticNet(
alpha=self.alpha,
l1_ratio=self.l1_ratio,
fit_intercept=self.fit_intercept,
normalize=self.normalize,
**self.kwargs,
)
)
10 changes: 4 additions & 6 deletions tests/test_models/test_linear_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ def test_repr_linear(model_class, model_class_repr):
"""Check __repr__ method of LinearPerSegmentModel and LinearMultiSegmentModel."""
kwargs = {"copy_X": True, "positive": True}
kwargs_repr = "copy_X = True, positive = True"
model = model_class(fit_intercept=True, normalize=False, **kwargs)
model = model_class(fit_intercept=True, **kwargs)
model_repr = model.__repr__()
true_repr = f"{model_class_repr}(fit_intercept = True, normalize = False, {kwargs_repr}, )"
true_repr = f"{model_class_repr}(fit_intercept = True, {kwargs_repr}, )"
assert model_repr == true_repr


Expand All @@ -115,11 +115,9 @@ def test_repr_elastic(model_class, model_class_repr):
"""Check __repr__ method of ElasticPerSegmentModel and ElasticMultiSegmentModel."""
kwargs = {"copy_X": True, "positive": True}
kwargs_repr = "copy_X = True, positive = True"
model = model_class(alpha=1.0, l1_ratio=0.5, fit_intercept=True, normalize=False, **kwargs)
model = model_class(alpha=1.0, l1_ratio=0.5, fit_intercept=True, **kwargs)
model_repr = model.__repr__()
true_repr = (
f"{model_class_repr}(alpha = 1.0, l1_ratio = 0.5, " f"fit_intercept = True, normalize = False, {kwargs_repr}, )"
)
true_repr = f"{model_class_repr}(alpha = 1.0, l1_ratio = 0.5, " f"fit_intercept = True, {kwargs_repr}, )"
assert model_repr == true_repr


Expand Down

1 comment on commit f898aea

@github-actions
Copy link

Choose a reason for hiding this comment

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

Please sign in to comment.