Skip to content

Commit 679b2ee

Browse files
FIX: warn when covariance matrix is singular in LinearExplainer (#4471)
* FIX: warn when covariance matrix is singular in LinearExplainer * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix mypy error --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent ce4abd7 commit 679b2ee

2 files changed

Lines changed: 35 additions & 62 deletions

File tree

‎shap/explainers/_linear.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,19 @@ def __init__(
199199
else:
200200
self.mean = np.array(np.mean(data, 0)).flatten() # assumes it is an array
201201
if self.feature_perturbation == "correlation_dependent":
202+
data_shape = np.shape(data)
203+
if len(data_shape) > 1:
204+
n_samples = data_shape[0]
205+
n_features = data_shape[1]
206+
if n_samples <= n_features:
207+
warnings.warn(
208+
f"The number of samples ({n_samples}) is less than or equal to "
209+
f"the number of features ({n_features}). This will produce a "
210+
"singular covariance matrix and may result in unreliable SHAP "
211+
"values when using feature_perturbation='correlation_dependent'.",
212+
UserWarning,
213+
stacklevel=2,
214+
)
202215
self.cov = np.cov(data, rowvar=False)
203216
# print(self.coef, self.mean.flatten(), self.intercept)
204217
# Note: mean can be numpy.matrixlib.defmatrix.matrix or numpy.matrix type depending on numpy version

‎tests/explainers/test_linear.py‎

Lines changed: 22 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import shap
1010
import shap.maskers
1111
from shap import maskers
12-
from shap.utils._exceptions import DimensionError, InvalidFeaturePerturbationError, InvalidModelError
12+
from shap.utils._exceptions import InvalidFeaturePerturbationError
1313

1414

1515
def test_tied_pair():
@@ -265,69 +265,29 @@ def test_interventional_multi_regression():
265265
assert np.allclose(shap_values.sum(1) + explainer.expected_value, outputs, atol=1e-6)
266266

267267

268-
def test_invalid_model_type_raises():
269-
# Passing a string instead of a model or (coef, intercept) tuple
270-
with pytest.raises(InvalidModelError, match="An unknown model type was passed"):
271-
shap.LinearExplainer("this_is_not_a_model", np.ones((10, 3)))
268+
def test_linear_explainer_warns_singular_covariance():
269+
"""LinearExplainer should warn when n_samples <= n_features."""
270+
import warnings
272271

272+
from sklearn.linear_model import LinearRegression
273273

274-
def test_dimension_error_on_high_dim_data():
275-
beta = np.array([1, 0, 0])
276-
X_background = np.ones((10, 3))
277-
explainer = shap.LinearExplainer((beta, 0), X_background)
278-
279-
# Create 3D data (e.g., an image batch shape)
280-
X_eval = np.ones((2, 2, 3))
281-
282-
with pytest.raises(DimensionError, match="Instance must have 1 or 2 dimensions"):
283-
explainer.shap_values(X_eval)
284-
285-
286-
def test_feature_dependence_deprecation():
287-
beta = np.array([1, 0])
288-
X_background = np.ones((10, 2))
289-
290-
with pytest.raises(ValueError, match="The option feature_dependence has been renamed to feature_perturbation!"):
291-
shap.LinearExplainer((beta, 0), X_background, feature_dependence="interventional")
292-
293-
294-
def test_missing_background_data():
295-
beta = np.array([1, 0])
296-
297-
bad_masker = shap.maskers.Independent(np.ones((1, 2)))
298-
bad_masker.data = None
299-
bad_masker.mean = None
300-
bad_masker.cov = None
274+
rng = np.random.default_rng(42)
275+
n_features = 10
276+
X_train = rng.normal(size=(8, n_features))
277+
y_train = X_train @ np.arange(1, n_features + 1, dtype=float)
278+
model = LinearRegression().fit(X_train, y_train)
301279

302-
with pytest.raises(ValueError, match="A background data distribution must be provided!"):
303-
shap.LinearExplainer((beta, 0), bad_masker)
304-
305-
306-
def test_explain_row_multiple_args():
307-
beta = np.array([1, 0])
308-
X_background = np.ones((10, 2))
309-
explainer = shap.LinearExplainer((beta, 0), X_background)
310-
311-
with pytest.raises(AssertionError, match="Only single-argument functions are supported by the Linear explainer!"):
312-
# Passing two data arguments instead of one
313-
explainer.explain_row(
314-
np.ones(2), np.ones(2), max_evals=100, main_effects=False, error_bounds=False, outputs=None, silent=True
280+
with warnings.catch_warnings(record=True) as w:
281+
warnings.simplefilter("always")
282+
shap.LinearExplainer(
283+
model,
284+
X_train,
285+
feature_perturbation="correlation_dependent",
315286
)
287+
user_warnings = [
288+
x for x in w if issubclass(x.category, UserWarning) and "singular covariance" in str(x.message).lower()
289+
]
316290

317-
318-
def test_parse_model_multiclass_single_class_fallback():
319-
class MockModel:
320-
def __init__(self):
321-
# Shape is (1, 2) which triggers the len(shape) > 1 and shape[0] == 1 condition
322-
self.coef_ = np.array([[0.5, 0.5]])
323-
# List-type intercept to trigger the TypeError fallback
324-
self.intercept_ = [0.1]
325-
326-
model = MockModel()
327-
X_background = np.ones((10, 2))
328-
329-
explainer = shap.LinearExplainer(model, X_background)
330-
331-
# Assert the parser correctly flattened the coef and grabbed the first element of the intercept
332-
assert np.array_equal(explainer.coef, np.array([0.5, 0.5]))
333-
assert explainer.intercept == 0.1
291+
assert len(user_warnings) == 1, (
292+
f"Expected a UserWarning about singular covariance matrix but got: {[str(x.message) for x in w]}"
293+
)

0 commit comments

Comments
 (0)