Files
2026-07-13 13:22:52 +08:00

43 lines
1.3 KiB
Python

import numpy as np
import pytest
from shap.utils import hclust
from shap.utils._exceptions import DimensionError
@pytest.mark.parametrize("linkage", ["single", "complete", "average"])
def test_hclust_runs(linkage):
# GH #3290
pytest.importorskip("xgboost")
X = np.column_stack((np.arange(1, 10), np.arange(100, 1000, step=100)))
y = np.where(X[:, 0] > 5, 1, 0)
# just check if clustered ran successfully (using xgboost_distances_r2)
clustered = hclust(X, y, linkage=linkage, random_state=0)
assert isinstance(clustered, np.ndarray)
assert clustered.shape == (1, 4)
# Check clustering runs if y=None (using scipy metrics)
clustered = hclust(X, linkage=linkage, random_state=0)
assert isinstance(clustered, np.ndarray)
assert clustered.shape == (1, 4)
@pytest.mark.parametrize(
"X",
[
np.arange(1, 10),
list(range(1, 10)),
],
)
def test_hclust_errors_on_input_shapes(X):
# hclust only accepts 2-d arrays for X
with pytest.raises(DimensionError):
hclust(X, random_state=0)
def test_hclust_errors_on_unknown_linkages():
X = np.column_stack((np.arange(1, 10), np.arange(100, 1000, step=100)))
with pytest.raises(ValueError, match=r"Unknown linkage type:"):
hclust(X, linkage="random-string", random_state=0) # type: ignore