Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Monotonicspline #710

Merged
merged 2 commits into from
Nov 6, 2024
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
14 changes: 6 additions & 8 deletions sklego/preprocessing/monotonicspline.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,6 @@ def fit(self, X, y=None):
)
for col in range(X.shape[1])
}
self.sorted_X = {col: np.sort(X[:, col]) for col in range(X.shape[1])}
self.sorted_X_output_ = {
col: self.spline_transformer_[col].transform(np.sort(X[:, col]).reshape(-1, 1)).cumsum(axis=0)
for col in range(X.shape[1])
}
self.sorted_idx_ = np.arange(X.shape[0]).astype(int)
return self

def transform(self, X):
Expand Down Expand Up @@ -95,6 +89,10 @@ def transform(self, X):
)
out = []
for col in range(X.shape[1]):
mapping = np.interp(X[:, col], self.sorted_X[col], self.sorted_idx_).astype(int)
out.append(self.sorted_X_output_[col][mapping])
out.append(
np.cumsum(
self.spline_transformer_[col].transform(X[:, [col]])[:, ::-1],
axis=1,
)
)
return np.concatenate(out, axis=1)
29 changes: 23 additions & 6 deletions tests/test_preprocessing/test_monospline.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
@pytest.mark.parametrize("knots", ["uniform", "quantile"])
def test_monotonic_spline_transformer(n_knots, degree, knots):
X = np.random.uniform(size=(100, 10))
transformer = MonotonicSplineTransformer(n_knots=n_knots, degree=degree, knots=knots)
transformer = MonotonicSplineTransformer(
n_knots=n_knots, degree=degree, knots=knots
)
transformer_sk = SplineTransformer(n_knots=n_knots, degree=degree, knots=knots)
transformer.fit(X)
transformer_sk.fit(X)
Expand All @@ -21,8 +23,23 @@ def test_monotonic_spline_transformer(n_knots, degree, knots):
# Both should have the same shape
assert out.shape == out_sk.shape

# Check that the monotonic variant always has a higher value than the non-monotonic variant
for col in range(out.shape[1]):
col_values = out[:, col]
col_values_sk = out_sk[:, col]
assert np.all(col_values >= col_values_sk), f"Column {col} is not monotonically increasing"
n_splines_per_feature = n_knots + degree - 1
assert out.shape[1] == X.shape[1] * n_splines_per_feature

# I splines should be bounded by 0 and 1
assert np.logical_or(out >= 0, np.isclose(out, 0)).all()
assert np.logical_or(out <= 1, np.isclose(out, 1)).all()

# The features should be monotonically increasing
for i in range(X.shape[1]):
feature = X[:, i]
sorted_out = out[
np.argsort(feature),
i * n_splines_per_feature : (i + 1) * n_splines_per_feature
]
differences = np.diff(sorted_out, axis=0)

# All differences should be greater or equal to zero upto floating point errors
assert np.logical_or(
np.greater_equal(differences, 0), np.isclose(differences, 0)
).all()