-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from lbventura/refactor-feature-importance-cal…
…culation
- Loading branch information
Showing
2 changed files
with
59 additions
and
53 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,65 @@ | ||
from cyclic_boosting import CBPoissonRegressor | ||
import numpy as np | ||
import pytest | ||
from typing import List | ||
import pandas as pd | ||
from typing import Dict, Tuple | ||
|
||
from cyclic_boosting.regression import CBBaseRegressor | ||
|
||
|
||
@pytest.fixture | ||
def expected_feature_importances() -> Dict[str, float]: | ||
return { | ||
"dayofweek": 0.08183693583617015, | ||
"L_ID": 0.14191802307396523, | ||
"PG_ID_3": 0.12016395453139928, | ||
"P_ID": 0.23511743026016937, | ||
"PROMOTION_TYPE": 0.10313172776022547, | ||
"price_ratio": 0.030753319720274865, | ||
"dayofyear": 0.09212591146822456, | ||
"P_ID_L_ID": 0.19495269734957096, | ||
} | ||
|
||
|
||
@pytest.fixture | ||
def expected_feature_importances() -> List[float]: | ||
return [ | ||
0.08183693583617015, | ||
0.14191802307396523, | ||
0.12016395453139928, | ||
0.23511743026016937, | ||
0.10313172776022547, | ||
0.030753319720274865, | ||
0.09212591146822456, | ||
0.19495269734957096, | ||
] | ||
def expected_feature_contributions() -> Dict[str, float]: | ||
return { | ||
"dayofweek": 1.0033225561393633, | ||
"L_ID": 0.9966915915554274, | ||
"PG_ID_3": 0.9962981313257777, | ||
"P_ID": 0.9581821452147931, | ||
"PROMOTION_TYPE": 0.9896018791652068, | ||
"price_ratio": 1.0, | ||
"dayofyear": 1.0506461325899688, | ||
"P_ID L_ID": 0.9140640045438535, | ||
} | ||
|
||
|
||
def test_poisson_regressor_feature_importance(prepare_data, features, feature_properties, expected_feature_importances): | ||
@pytest.fixture(scope="session") | ||
def estimator_data(prepare_data, features, feature_properties) -> Tuple[CBBaseRegressor, pd.DataFrame]: | ||
X, y = prepare_data | ||
est = CBPoissonRegressor( | ||
feature_groups=features, | ||
feature_properties=feature_properties, | ||
) | ||
est.fit(X, y) | ||
norm_feature_importances = est.get_feature_importances() | ||
return est, X | ||
|
||
assert [ele[0].feature_group for ele in norm_feature_importances.keys()] == [ | ||
("dayofweek",), | ||
("L_ID",), | ||
("PG_ID_3",), | ||
("P_ID",), | ||
("PROMOTION_TYPE",), | ||
("price_ratio",), | ||
("dayofyear",), | ||
("P_ID", "L_ID"), | ||
] | ||
|
||
for ind, f_imp in enumerate(norm_feature_importances.values()): | ||
np.testing.assert_almost_equal(f_imp, expected_feature_importances[ind], 4) | ||
np.testing.assert_almost_equal(sum(norm_feature_importances.values()), 1.0, 3) | ||
def test_feature_importance(estimator_data, expected_feature_importances): | ||
estimator, _ = estimator_data | ||
norm_feature_importances = estimator.get_feature_importances() | ||
|
||
for feature_name, feature_importance in norm_feature_importances.items(): | ||
assert feature_name in expected_feature_importances.keys() | ||
np.testing.assert_almost_equal(feature_importance, expected_feature_importances[feature_name], 4) | ||
np.testing.assert_almost_equal(sum(norm_feature_importances.values()), 1.0, 3) | ||
|
||
def test_poisson_regressor_feature_contributions(prepare_data, features, feature_properties): | ||
X, y = prepare_data | ||
est = CBPoissonRegressor( | ||
feature_groups=features, | ||
feature_properties=feature_properties, | ||
) | ||
est.fit(X, y) | ||
feature_contributions = est.get_feature_contributions(X) | ||
|
||
assert [ele for ele in feature_contributions.keys()] == [ | ||
"dayofweek", | ||
"L_ID", | ||
"PG_ID_3", | ||
"P_ID", | ||
"PROMOTION_TYPE", | ||
"price_ratio", | ||
"dayofyear", | ||
"P_ID L_ID", | ||
] | ||
def test_feature_contributions(estimator_data, expected_feature_contributions): | ||
estimator, X = estimator_data | ||
feature_contributions = estimator.get_feature_contributions(X) | ||
|
||
np.testing.assert_almost_equal(feature_contributions["dayofweek"].mean(), 1.003, 3) | ||
np.testing.assert_almost_equal(feature_contributions["P_ID"].mean(), 0.958, 3) | ||
for feature_name, feature_contribution in feature_contributions.items(): | ||
assert feature_name in expected_feature_contributions.keys() | ||
np.testing.assert_almost_equal(feature_contribution.mean(), expected_feature_contributions[feature_name], 3) |