Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -559,14 +559,14 @@ def merge_ebms(models):
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=RuntimeWarning)

# nanmin/nanmax give NaN for features that are categorical in every model.
# Keep those rows like a fitted EBM does, so the attribute exists even when
# all features are categorical.
min_feature_vals = np.nanmin(min_feature_vals, axis=0)
max_feature_vals = np.nanmax(max_feature_vals, axis=0)
if any(not isnan(val) for val in min_feature_vals) or any(
not isnan(val) for val in max_feature_vals
):
ebm.feature_bounds_ = np.array(
list(zip(min_feature_vals, max_feature_vals)), np.float64
)
ebm.feature_bounds_ = np.array(
list(zip(min_feature_vals, max_feature_vals)), np.float64
)

if not is_dp:
if all(
Expand Down
26 changes: 26 additions & 0 deletions python/interpret-core/tests/glassbox/ebm/test_merge_ebms.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import warnings

import numpy as np
import pandas as pd
from interpret.glassbox import (
ExplainableBoostingClassifier,
ExplainableBoostingRegressor,
Expand Down Expand Up @@ -467,3 +468,28 @@ def test_merge_ebms_predictions_unchanged():
assert np.isfinite(probabilities).all()
assert (probabilities >= 0.0).all()
assert (probabilities <= 1.0).all()


def test_merge_ebms_all_categorical():
"""Merging EBMs whose features are all categorical keeps ``feature_bounds_``.

The merged bounds are NaN for categoricals, as in a fitted EBM, and the
merged model predicts the bag-weighted average of the input models.
"""
rng = np.random.default_rng(0)
X = pd.DataFrame(
{"c1": rng.choice(list("abcd"), 400), "c2": rng.choice(list("xyz"), 400)}
)
y = (X["c1"] == "a") + (X["c2"] == "z") * (X["c1"] == "b") + rng.normal(0, 0.1, 400)

ebm1 = ExplainableBoostingRegressor(outer_bags=3, random_state=1)
ebm1.fit(X[:200], y[:200])
ebm2 = ExplainableBoostingRegressor(outer_bags=5, random_state=2)
ebm2.fit(X[200:], y[200:])

merged = merge_ebms([ebm1, ebm2])

assert np.isnan(merged.feature_bounds_).all()
np.testing.assert_allclose(
merged.predict(X), (3 * ebm1.predict(X) + 5 * ebm2.predict(X)) / 8
)
Loading