Skip to content

Commit

Permalink
Revert "Monotonicspline (#710)"
Browse files Browse the repository at this point in the history
This reverts commit 2af1b6d.
  • Loading branch information
koaning authored Nov 6, 2024
1 parent 2af1b6d commit 4727e49
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 29 deletions.
14 changes: 8 additions & 6 deletions sklego/preprocessing/monotonicspline.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ 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 @@ -89,10 +95,6 @@ def transform(self, X):
)
out = []
for col in range(X.shape[1]):
out.append(
np.cumsum(
self.spline_transformer_[col].transform(X[:, [col]])[:, ::-1],
axis=1,
)
)
mapping = np.interp(X[:, col], self.sorted_X[col], self.sorted_idx_).astype(int)
out.append(self.sorted_X_output_[col][mapping])
return np.concatenate(out, axis=1)
29 changes: 6 additions & 23 deletions tests/test_preprocessing/test_monospline.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
@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 @@ -23,23 +21,8 @@ def test_monotonic_spline_transformer(n_knots, degree, knots):
# Both should have the same shape
assert out.shape == out_sk.shape

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()
# 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"

0 comments on commit 4727e49

Please sign in to comment.