-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Made use of non_null params consistent for linear quantile model…
… so null columns are handled correctly. (#542) * fix: Made use of non_null params consistent for linear quantile model so null columns are handled correctly. Signed-off-by: Egor Dmitriev <egor.dmitriev@alliander.com> * fix: Fixed (test) issues introduced by xgboost / sklearn upgrade. Signed-off-by: Egor Dmitriev <egor.dmitriev@alliander.com> * Format Python code with Black Signed-off-by: black <action@github.com> --------- Signed-off-by: Egor Dmitriev <egor.dmitriev@alliander.com> Signed-off-by: black <action@github.com> Co-authored-by: black <action@github.com>
- Loading branch information
1 parent
42f9ad5
commit b16e1d1
Showing
4 changed files
with
103 additions
and
15 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
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
61 changes: 61 additions & 0 deletions
61
test/unit/feature_engineering/test_missing_values_transformer.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,61 @@ | ||
# SPDX-FileCopyrightText: 2017-2023 Contributors to the OpenSTEF project <korte.termijn.prognoses@alliander.com> # noqa E501> | ||
# | ||
# SPDX-License-Identifier: MPL-2.0 | ||
from test.unit.utils.base import BaseTestCase | ||
|
||
import unittest | ||
import pandas as pd | ||
import numpy as np | ||
from sklearn.exceptions import NotFittedError | ||
from openstef.feature_engineering.missing_values_transformer import ( | ||
MissingValuesTransformer, | ||
) | ||
|
||
|
||
class MissingValuesTransformerTests(BaseTestCase): | ||
def setUp(self): | ||
self.data = pd.DataFrame( | ||
{"A": [1, np.nan, 3], "B": [4, 5, np.nan], "C": [np.nan, np.nan, np.nan]} | ||
) | ||
|
||
def test_imputation_with_mean_strategy_fills_missing_values(self): | ||
transformer = MissingValuesTransformer(imputation_strategy="mean") | ||
transformed = transformer.fit_transform(self.data) | ||
self.assertEqual(transformed.isnull().sum().sum(), 0) | ||
self.assertAlmostEqual(transformed.loc[1, "A"], 2) | ||
self.assertAlmostEqual(transformed.loc[2, "B"], 4.5) | ||
|
||
def test_imputation_with_constant_strategy_fills_missing_values(self): | ||
transformer = MissingValuesTransformer( | ||
imputation_strategy="constant", fill_value=0 | ||
) | ||
transformed = transformer.fit_transform(self.data) | ||
self.assertEqual(transformed.isnull().sum().sum(), 0) | ||
self.assertEqual(transformed.loc[1, "A"], 0) | ||
self.assertEqual(transformed.loc[2, "B"], 0) | ||
|
||
def test_columns_always_null_are_removed(self): | ||
transformer = MissingValuesTransformer() | ||
transformer.fit(self.data) | ||
self.assertNotIn("C", transformer.non_null_feature_names) | ||
|
||
def test_non_dataframe_input_is_converted_and_processed(self): | ||
transformer = MissingValuesTransformer(imputation_strategy="mean") | ||
array = np.array([[1, np.nan], [np.nan, 2]]) | ||
transformed = transformer.fit_transform(array) | ||
self.assertIsInstance(transformed, pd.DataFrame) | ||
self.assertEqual(transformed.isnull().sum().sum(), 0) | ||
|
||
def test_fitting_transformer_without_strategy_keeps_data_unchanged(self): | ||
transformer = MissingValuesTransformer() | ||
transformed = transformer.fit_transform(self.data) | ||
pd.testing.assert_frame_equal(transformed, self.data.drop(columns=["C"])) | ||
|
||
def test_calling_transform_before_fit_raises_error(self): | ||
transformer = MissingValuesTransformer() | ||
with self.assertRaises(NotFittedError): | ||
transformer.transform(self.data) | ||
|
||
def test_imputation_with_unsupported_strategy_raises_value_error(self): | ||
with self.assertRaises(ValueError): | ||
MissingValuesTransformer(imputation_strategy="unsupported") |