|
9 | 9 | import shap |
10 | 10 | import shap.maskers |
11 | 11 | from shap import maskers |
12 | | -from shap.utils._exceptions import DimensionError, InvalidFeaturePerturbationError, InvalidModelError |
| 12 | +from shap.utils._exceptions import InvalidFeaturePerturbationError |
13 | 13 |
|
14 | 14 |
|
15 | 15 | def test_tied_pair(): |
@@ -265,69 +265,29 @@ def test_interventional_multi_regression(): |
265 | 265 | assert np.allclose(shap_values.sum(1) + explainer.expected_value, outputs, atol=1e-6) |
266 | 266 |
|
267 | 267 |
|
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 |
272 | 271 |
|
| 272 | + from sklearn.linear_model import LinearRegression |
273 | 273 |
|
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) |
301 | 279 |
|
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", |
315 | 286 | ) |
| 287 | + user_warnings = [ |
| 288 | + x for x in w if issubclass(x.category, UserWarning) and "singular covariance" in str(x.message).lower() |
| 289 | + ] |
316 | 290 |
|
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