-
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.
Merge branch 'main' into 167-set-learning-rate-of-adaboost
- Loading branch information
Showing
2 changed files
with
88 additions
and
23 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
55 changes: 42 additions & 13 deletions
55
tests/safeds/ml/classical/regression/test_elastic_net_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 |
---|---|---|
@@ -1,39 +1,68 @@ | ||
import pytest | ||
from safeds.data.tabular.containers import Table | ||
from safeds.ml.classical.regression._elastic_net_regression import ElasticNetRegression | ||
from safeds.ml.classical.regression import ElasticNetRegression | ||
|
||
|
||
def test_lasso_ratio_valid() -> None: | ||
def test_should_throw_value_error_alpha() -> None: | ||
with pytest.raises(ValueError, match="alpha must be non-negative"): | ||
ElasticNetRegression(alpha=-1.0) | ||
|
||
|
||
def test_should_throw_warning_alpha() -> 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." | ||
), | ||
): | ||
ElasticNetRegression(alpha=0.0) | ||
|
||
|
||
def test_should_give_alpha_to_sklearn() -> None: | ||
training_set = Table.from_dict({"col1": [1, 2, 3, 4], "col2": [1, 2, 3, 4]}) | ||
tagged_training_set = training_set.tag_columns(target_name="col1", feature_names=["col2"]) | ||
|
||
elastic_net_regression = ElasticNetRegression(alpha=1.0).fit(tagged_training_set) | ||
assert elastic_net_regression._wrapped_regressor is not None | ||
assert elastic_net_regression._wrapped_regressor.alpha == elastic_net_regression._alpha | ||
|
||
|
||
def test_should_give_lasso_ratio_to_sklearn() -> None: | ||
training_set = Table.from_dict({"col1": [1, 2, 3, 4], "col2": [1, 2, 3, 4]}) | ||
tagged_training_set = training_set.tag_columns(target_name="col1", feature_names=["col2"]) | ||
lasso_ratio = 0.3 | ||
|
||
elastic_net_regression = ElasticNetRegression(lasso_ratio).fit(tagged_training_set) | ||
elastic_net_regression = ElasticNetRegression(lasso_ratio=lasso_ratio).fit(tagged_training_set) | ||
assert elastic_net_regression._wrapped_regressor is not None | ||
assert elastic_net_regression._wrapped_regressor.l1_ratio == lasso_ratio | ||
|
||
|
||
def test_lasso_ratio_invalid() -> None: | ||
def test_should_throw_value_error_lasso_ratio() -> None: | ||
with pytest.raises(ValueError, match="lasso_ratio must be between 0 and 1."): | ||
ElasticNetRegression(-1) | ||
ElasticNetRegression(lasso_ratio=-1.0) | ||
|
||
|
||
def test_lasso_ratio_zero() -> None: | ||
def test_should_throw_warning_lasso_ratio_zero() -> None: | ||
with pytest.warns( | ||
UserWarning, | ||
match="ElasticNetRegression with lasso_ratio = 0 is essentially RidgeRegression." | ||
" Use RidgeRegression instead for better numerical stability.", | ||
match=( | ||
"ElasticNetRegression with lasso_ratio = 0 is essentially RidgeRegression." | ||
" Use RidgeRegression instead for better numerical stability." | ||
), | ||
): | ||
ElasticNetRegression(0) | ||
ElasticNetRegression(lasso_ratio=0) | ||
|
||
|
||
def test_lasso_ratio_one() -> None: | ||
def test_should_throw_warning_lasso_ratio_one() -> None: | ||
with pytest.warns( | ||
UserWarning, | ||
match="ElasticNetRegression with lasso_ratio = 0 is essentially LassoRegression." | ||
" Use LassoRegression instead for better numerical stability.", | ||
match=( | ||
"ElasticNetRegression with lasso_ratio = 0 is essentially LassoRegression." | ||
" Use LassoRegression instead for better numerical stability." | ||
), | ||
): | ||
ElasticNetRegression(1) | ||
ElasticNetRegression(lasso_ratio=1) | ||
|
||
|
||
# (Default parameter is tested in `test_regressor.py`.) |