|
| 1 | +__author__ = "Jeroen Van Der Donckt, Jarne Verhaeghe" |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import pandas as pd |
| 5 | +from catboost import CatBoostClassifier, CatBoostRegressor |
| 6 | +from sklearn.ensemble import RandomForestClassifier, RandomForestRegressor |
| 7 | + |
| 8 | +from powershap import PowerShap |
| 9 | + |
| 10 | +from .conftest import dummy_classification, dummy_regression |
| 11 | +from sklearn.pipeline import Pipeline |
| 12 | +from sklearn.pipeline import make_pipeline |
| 13 | +from sklearn.preprocessing import FunctionTransformer |
| 14 | + |
| 15 | +def test_pipeline_catboost_class_powershap(dummy_classification): |
| 16 | + X, y = dummy_classification |
| 17 | + n_informative = sum([c.startswith("informative") for c in X.columns]) |
| 18 | + assert n_informative > 0, "No informative columns in the dummy data!" |
| 19 | + |
| 20 | + DummyScaler = FunctionTransformer(lambda x: x) |
| 21 | + |
| 22 | + selector = PowerShap( |
| 23 | + model=make_pipeline(DummyScaler, CatBoostClassifier(n_estimators=250, verbose=0)), power_iterations=15, automatic=False |
| 24 | + ) |
| 25 | + |
| 26 | + selector.fit(X, y) |
| 27 | + selected_feats = selector.transform(X) |
| 28 | + |
| 29 | + assert len(selected_feats.columns) == n_informative |
| 30 | + assert all([c.startswith("informative") for c in selected_feats.columns]) |
| 31 | + |
| 32 | + |
| 33 | +def test_pipeline_catboost_regr_powershap(dummy_regression): |
| 34 | + X, y = dummy_regression |
| 35 | + n_informative = sum([c.startswith("informative") for c in X.columns]) |
| 36 | + assert n_informative > 0, "No informative columns in the dummy data!" |
| 37 | + |
| 38 | + DummyScaler = FunctionTransformer(lambda x: x) |
| 39 | + |
| 40 | + selector = PowerShap( |
| 41 | + model=make_pipeline(DummyScaler, CatBoostRegressor(n_estimators=250, verbose=0)), power_iterations=15, automatic=False |
| 42 | + ) |
| 43 | + |
| 44 | + selector.fit(X, y) |
| 45 | + selected_feats = selector.transform(X) |
| 46 | + |
| 47 | + assert len(selected_feats.columns) == n_informative |
| 48 | + assert all([c.startswith("informative") for c in selected_feats.columns]) |
| 49 | + |
| 50 | + |
| 51 | +def test_pipeline_catboost_handle_nans(dummy_classification): |
| 52 | + X, y = dummy_classification |
| 53 | + X.iloc[:5] = None |
| 54 | + X["nan_col"] = None |
| 55 | + assert np.any(pd.isna(X)) |
| 56 | + n_informative = sum([c.startswith("informative") for c in X.columns]) |
| 57 | + assert n_informative > 0, "No informative columns in the dummy data!" |
| 58 | + |
| 59 | + DummyScaler = FunctionTransformer(lambda x: x) |
| 60 | + |
| 61 | + selector = PowerShap( |
| 62 | + model=make_pipeline(DummyScaler, CatBoostClassifier(n_estimators=10, verbose=0)), power_iterations=15 |
| 63 | + ) |
| 64 | + |
| 65 | + selector.fit(X, y) |
| 66 | + selected_feats = selector.transform(X) |
| 67 | + |
| 68 | + assert len(selected_feats.columns) == n_informative |
| 69 | + assert all([c.startswith("informative") for c in selected_feats.columns]) |
| 70 | + |
| 71 | + |
| 72 | +def test_pipeline_catboost_handle_infs(dummy_classification): |
| 73 | + X, y = dummy_classification |
| 74 | + X.iloc[:5] = np.Inf |
| 75 | + X["inf_col"] = np.Inf |
| 76 | + assert np.any(X.isin([np.inf, -np.inf])) |
| 77 | + n_informative = sum([c.startswith("informative") for c in X.columns]) |
| 78 | + assert n_informative > 0, "No informative columns in the dummy data!" |
| 79 | + |
| 80 | + DummyScaler = FunctionTransformer(lambda x: x) |
| 81 | + |
| 82 | + selector = PowerShap( |
| 83 | + model=make_pipeline(DummyScaler, CatBoostClassifier(n_estimators=10, verbose=0)), power_iterations=15 |
| 84 | + ) |
| 85 | + |
| 86 | + selector.fit(X, y) |
| 87 | + selected_feats = selector.transform(X) |
| 88 | + |
| 89 | + assert len(selected_feats.columns) == n_informative |
| 90 | + assert all([c.startswith("informative") for c in selected_feats.columns]) |
| 91 | + |
| 92 | + |
| 93 | +def test_pipeline_catboost_handle_infs_nans(dummy_classification): |
| 94 | + X, y = dummy_classification |
| 95 | + X.iloc[:5] = np.Inf |
| 96 | + X.iloc[5:10] = None |
| 97 | + X["inf_col"] = np.Inf |
| 98 | + X["nan_col"] = None |
| 99 | + assert np.any(X.isin([np.inf, -np.inf])) |
| 100 | + assert np.any(pd.isna(X)) |
| 101 | + n_informative = sum([c.startswith("informative") for c in X.columns]) |
| 102 | + assert n_informative > 0, "No informative columns in the dummy data!" |
| 103 | + |
| 104 | + DummyScaler = FunctionTransformer(lambda x: x) |
| 105 | + |
| 106 | + selector = PowerShap( |
| 107 | + model=make_pipeline(DummyScaler, CatBoostClassifier(n_estimators=10, verbose=0)), power_iterations=15 |
| 108 | + ) |
| 109 | + |
| 110 | + selector.fit(X, y) |
| 111 | + selected_feats = selector.transform(X) |
| 112 | + |
| 113 | + assert len(selected_feats.columns) == n_informative |
| 114 | + assert all([c.startswith("informative") for c in selected_feats.columns]) |
| 115 | + |
| 116 | + |
| 117 | +def test_pipeline_catboost_handle_strings(dummy_classification): |
| 118 | + X, y = dummy_classification |
| 119 | + X["cat"] = "miauw" |
| 120 | + n_informative = sum([c.startswith("informative") for c in X.columns]) |
| 121 | + assert n_informative > 0, "No informative columns in the dummy data!" |
| 122 | + |
| 123 | + DummyScaler = FunctionTransformer(lambda x: x) |
| 124 | + |
| 125 | + selector = PowerShap( |
| 126 | + model=make_pipeline(DummyScaler,CatBoostClassifier(n_estimators=30, verbose=0, cat_features=[X.shape[1] - 1])), |
| 127 | + power_iterations=15 |
| 128 | + ) |
| 129 | + |
| 130 | + selector.fit(X, y) |
| 131 | + selected_feats = selector.transform(X) |
| 132 | + |
| 133 | + assert len(selected_feats.columns) == n_informative |
| 134 | + assert all([c.startswith("informative") for c in selected_feats.columns]) |
| 135 | + |
| 136 | + |
| 137 | +def test_pipeline_ensemble_class_powershap(dummy_classification): |
| 138 | + X, y = dummy_classification |
| 139 | + n_informative = sum([c.startswith("informative") for c in X.columns]) |
| 140 | + assert n_informative > 0, "No informative columns in the dummy data!" |
| 141 | + |
| 142 | + DummyScaler = FunctionTransformer(lambda x: x) |
| 143 | + |
| 144 | + selector = PowerShap( |
| 145 | + model=make_pipeline(DummyScaler,RandomForestClassifier(n_estimators=25)), power_iterations=15, automatic=False |
| 146 | + ) |
| 147 | + |
| 148 | + selector.fit(X, y) |
| 149 | + selected_feats = selector.transform(X) |
| 150 | + |
| 151 | + assert len(selected_feats.columns) >= n_informative |
| 152 | + assert sum([c.startswith("informative") for c in selected_feats.columns]) == n_informative |
| 153 | + |
| 154 | + |
| 155 | +def test_pipeline_ensemble_regr_powershap(dummy_regression): |
| 156 | + X, y = dummy_regression |
| 157 | + n_informative = sum([c.startswith("informative") for c in X.columns]) |
| 158 | + assert n_informative > 0, "No informative columns in the dummy data!" |
| 159 | + |
| 160 | + DummyScaler = FunctionTransformer(lambda x: x) |
| 161 | + |
| 162 | + selector = PowerShap( |
| 163 | + model=make_pipeline(DummyScaler,RandomForestRegressor(n_estimators=25)), power_iterations=15, automatic=False |
| 164 | + ) |
| 165 | + |
| 166 | + selector.fit(X, y) |
| 167 | + selected_feats = selector.transform(X) |
| 168 | + |
| 169 | + assert len(selected_feats.columns) >= n_informative |
| 170 | + assert sum([c.startswith("informative") for c in selected_feats.columns]) == n_informative |
| 171 | + |
| 172 | +def test_pipeline_catboost_class_standardscaler_powershap(dummy_classification): |
| 173 | + from sklearn.preprocessing import StandardScaler |
| 174 | + |
| 175 | + X, y = dummy_classification |
| 176 | + n_informative = sum([c.startswith("informative") for c in X.columns]) |
| 177 | + assert n_informative > 0, "No informative columns in the dummy data!" |
| 178 | + |
| 179 | + selector = PowerShap( |
| 180 | + model=make_pipeline(StandardScaler, CatBoostClassifier(n_estimators=250, verbose=0)), power_iterations=15, automatic=False |
| 181 | + ) |
| 182 | + |
| 183 | + selector.fit(X, y) |
| 184 | + selected_feats = selector.transform(X) |
| 185 | + |
| 186 | + assert len(selected_feats.columns) == n_informative |
| 187 | + assert all([c.startswith("informative") for c in selected_feats.columns]) |
| 188 | + |
| 189 | +def test_pipeline_catboost_class_maxabsscalerr_robustscaler_powershap(dummy_classification): |
| 190 | + from sklearn.preprocessing import MaxAbsScaler, RobustScaler |
| 191 | + |
| 192 | + X, y = dummy_classification |
| 193 | + n_informative = sum([c.startswith("informative") for c in X.columns]) |
| 194 | + assert n_informative > 0, "No informative columns in the dummy data!" |
| 195 | + |
| 196 | + selector = PowerShap( |
| 197 | + model=make_pipeline(MaxAbsScaler, RobustScaler, CatBoostClassifier(n_estimators=250, verbose=0)), power_iterations=15, automatic=False |
| 198 | + ) |
| 199 | + |
| 200 | + selector.fit(X, y) |
| 201 | + selected_feats = selector.transform(X) |
| 202 | + |
| 203 | + assert len(selected_feats.columns) == n_informative |
| 204 | + assert all([c.startswith("informative") for c in selected_feats.columns]) |
0 commit comments