-
Notifications
You must be signed in to change notification settings - Fork 80
Fix mrmr working with categoricals #1311
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,43 @@ | ||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
import pandas as pd | ||
import pytest | ||
|
||
from etna.datasets import TSDataset | ||
from etna.datasets import duplicate_data | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def close_plots(): | ||
yield | ||
plt.close() | ||
|
||
|
||
@pytest.fixture | ||
def exog_and_target_dfs(): | ||
seg = ["a"] * 30 + ["b"] * 30 | ||
time = list(pd.date_range("2020-01-01", "2021-01-01")[:30]) | ||
timestamps = time * 2 | ||
target = np.arange(60) | ||
df = pd.DataFrame({"segment": seg, "timestamp": timestamps, "target": target}) | ||
ts = TSDataset.to_dataset(df) | ||
|
||
cast = ["1.1"] * 10 + ["2"] * 9 + [None] + ["56.1"] * 10 | ||
no_cast = ["1.1"] * 10 + ["two"] * 10 + ["56.1"] * 10 | ||
none = [1] * 10 + [2] * 10 + [56.1] * 10 | ||
none[10] = None | ||
df = pd.DataFrame( | ||
{ | ||
"timestamp": time, | ||
"exog1": np.arange(100, 70, -1), | ||
"exog2": np.sin(np.arange(30) / 10), | ||
"exog3": np.exp(np.arange(30)), | ||
"cast": cast, | ||
"no_cast": no_cast, | ||
"none": none, | ||
} | ||
) | ||
df["cast"] = df["cast"].astype("category") | ||
df["no_cast"] = df["no_cast"].astype("category") | ||
df_exog = duplicate_data(df, segments=["a", "b"]) | ||
return ts, df_exog |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
from typing import Dict | ||
from unittest.mock import Mock | ||
|
||
import numpy as np | ||
import pandas as pd | ||
|
@@ -32,6 +33,12 @@ def df_with_regressors() -> Dict[str, pd.DataFrame]: | |
regressor = df_regressors_useless[df_regressors_useless["segment"] == segment]["target"].values | ||
df_exog[f"regressor_useless_{i}"] = regressor | ||
|
||
# useless categorical regressor | ||
num_cat_useless = 3 | ||
for i in range(num_cat_useless): | ||
df_exog[f"categorical_regressor_useless_{i}"] = i | ||
df_exog[f"categorical_regressor_useless_{i}"] = df_exog[f"categorical_regressor_useless_{i}"].astype("category") | ||
|
||
# useful regressors: the same as target but with little noise | ||
df_regressors_useful = df.copy() | ||
sampler = RandomState(seed=2).normal | ||
|
@@ -174,3 +181,17 @@ def test_fast_redundancy_deprecation_warning(df_with_regressors): | |
relevance_table = ModelRelevanceTable()(df=df, df_exog=regressors, model=RandomForestRegressor()) | ||
with pytest.warns(DeprecationWarning, match="Option `fast_redundancy=False` was added for backward compatibility"): | ||
mrmr(relevance_table=relevance_table, regressors=regressors, top_k=2, fast_redundancy=False) | ||
|
||
|
||
@pytest.mark.parametrize("fast_redundancy", [True, False]) | ||
def test_mrmr_with_categorical_regressor(df_with_regressors, fast_redundancy): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
df, regressors = df_with_regressors["df"], df_with_regressors["regressors"] | ||
relevance_table = ModelRelevanceTable()(df=df, df_exog=regressors, model=RandomForestRegressor()) | ||
mrmr(relevance_table=relevance_table, regressors=regressors, top_k=len(regressors), fast_redundancy=fast_redundancy) | ||
|
||
|
||
@pytest.mark.parametrize("fast_redundancy", [True, False]) | ||
def test_mrmr_with_uncastable_categorical_regressor_fails(exog_and_target_dfs, fast_redundancy): | ||
df, regressors = exog_and_target_dfs | ||
with pytest.raises(ValueError, match="Only convertible to float features are allowed!"): | ||
mrmr(relevance_table=Mock(), regressors=regressors, top_k=len(regressors), fast_redundancy=fast_redundancy) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
regressor -> regressors?