-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added alpha parameter to
RidgeRegression
(#231)
Closes #164 . ### Summary of Changes Added alpha parameter to `RidgeRegression` for regularization. --------- Co-authored-by: sibre28 <86068340+sibre28@users.noreply.github.com> Co-authored-by: megalinter-bot <129584137+megalinter-bot@users.noreply.github.com> Co-authored-by: Lars Reimann <mail@larsreimann.com>
- Loading branch information
1 parent
9ffaf55
commit 1ddc948
Showing
3 changed files
with
63 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
tests/safeds/ml/classical/regression/test_ridge_regression.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import pytest | ||
from safeds.data.tabular.containers import Table | ||
from safeds.ml.classical.regression import RidgeRegression | ||
|
||
|
||
def test_should_raise_if_alpha_is_negative() -> None: | ||
with pytest.raises(ValueError, match="alpha must be non-negative"): | ||
RidgeRegression(alpha=-1.0) | ||
|
||
|
||
def test_should_warn_if_alpha_is_zero() -> None: | ||
with pytest.warns( | ||
UserWarning, | ||
match=( | ||
"Setting alpha to zero makes this model equivalent to LinearRegression. You " | ||
"should use LinearRegression instead for better numerical stability." | ||
), | ||
): | ||
RidgeRegression(alpha=0.0) | ||
|
||
|
||
def test_should_pass_alpha_to_fitted_regressor() -> None: | ||
regressor = RidgeRegression(alpha=1.0) | ||
fitted_regressor = regressor.fit(Table.from_dict({"A": [1, 2, 4], "B": [1, 2, 3]}).tag_columns("B")) | ||
assert regressor._alpha == fitted_regressor._alpha | ||
|
||
|
||
def test_should_pass_alpha_to_sklearn() -> None: | ||
regressor = RidgeRegression(alpha=1.0) | ||
fitted_regressor = regressor.fit(Table.from_dict({"A": [1, 2, 4], "B": [1, 2, 3]}).tag_columns("B")) | ||
assert fitted_regressor._wrapped_regressor is not None | ||
assert fitted_regressor._wrapped_regressor.alpha == fitted_regressor._alpha |