Skip to content
Merged
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
2 changes: 2 additions & 0 deletions src/splinator/estimators.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,8 @@ def fit(self, X, y, sample_weight=None):
# Validate sample_weight
if sample_weight is not None:
sample_weight = np.asarray(sample_weight)
if sample_weight.ndim != 1:
raise ValueError("sample_weight must be a 1-D array")
if sample_weight.shape[0] != X.shape[0]:
raise ValueError(
f"sample_weight has {sample_weight.shape[0]} samples, "
Expand Down
1 change: 0 additions & 1 deletion src/splinator/metric_wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,3 @@ def torch_wrapper(y_true, y_pred, sample_weight=None):
f"Unknown framework: {framework}. "
f"Supported: 'sklearn', 'xgboost', 'lightgbm', 'pytorch'"
)

14 changes: 13 additions & 1 deletion src/splinator/monotonic_spline.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,20 @@ def _weighted_quantile(values, quantiles, sample_weight=None):

if sample_weight is None:
return np.quantile(values, quantiles)

sample_weight = np.asarray(sample_weight)
if sample_weight.shape != values.shape:
raise ValueError("sample_weight must have the same shape as values")

positive_weight = sample_weight > 0
if not np.any(positive_weight):
raise ValueError("sample_weight must contain at least one positive value")

values = values[positive_weight]
sample_weight = sample_weight[positive_weight]
if np.all(sample_weight == sample_weight[0]):
return np.quantile(values, quantiles)

sorted_indices = np.argsort(values)
sorted_values = values[sorted_indices]
sorted_weights = sample_weight[sorted_indices]
Expand Down
24 changes: 23 additions & 1 deletion tests/test_sample_weight.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,16 @@ def test_uniform_weights_matches_numpy(self):
result = _weighted_quantile(X, quantiles, sample_weight=weights)
expected = np.quantile(X, quantiles)

np.testing.assert_array_almost_equal(result, expected, decimal=1)
np.testing.assert_array_almost_equal(result, expected)

def test_uniform_weights_preserve_interpolation(self):
"""Uniform weights should preserve NumPy's interpolated quantiles."""
X = np.array([0.0, 10.0])
weights = np.ones(2)

result = _weighted_quantile(X, [0.5], sample_weight=weights)

np.testing.assert_array_equal(result, np.array([5.0]))

def test_doubled_weights_equivalent_to_duplication(self):
"""Doubling a sample's weight should be like duplicating it."""
Expand Down Expand Up @@ -69,6 +78,15 @@ def test_knots_with_weights(self):

assert len(knots) == 4

def test_uniform_weights_match_unweighted_knots(self):
"""Uniform weights should not change knot placement."""
X = np.array([0.0, 10.0])
weights = np.ones(2)

knots = _fit_knots(X, num_knots=2, sample_weight=weights)

np.testing.assert_array_equal(knots, _fit_knots(X, num_knots=2))


class TestLossGradHessWithWeights:
"""Test LossGradHess class with sample weights."""
Expand Down Expand Up @@ -213,6 +231,10 @@ def test_sample_weight_validation(self):
# Wrong length
with pytest.raises(ValueError, match="sample_weight has .* samples"):
model.fit(X, y, sample_weight=np.ones(50))

# Wrong dimensionality
with pytest.raises(ValueError, match="1-D"):
model.fit(X, y, sample_weight=np.ones((100, 2)))

# Negative weights
with pytest.raises(ValueError, match="non-negative"):
Expand Down
Loading