Skip to content

Commit

Permalink
base-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
koaning committed Nov 6, 2024
1 parent f77fc4e commit 0ab9442
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
4 changes: 4 additions & 0 deletions sklego/preprocessing/monotonicspline.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def fit(self, X, y=None):
)
for col in range(X.shape[1])
}
self.n_features_in_ = X.shape[1]
return self

def transform(self, X):
Expand All @@ -87,6 +88,9 @@ def transform(self, X):
dtype=FLOAT_DTYPES,
estimator=self,
)
if X.shape[1] != self.n_features_in_:
raise ValueError("Number of features going into .transform() do not match number going into .fit().")

out = []
for col in range(X.shape[1]):
out.append(
Expand Down
19 changes: 9 additions & 10 deletions tests/test_preprocessing/test_monospline.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import numpy as np
import pytest
from sklearn.preprocessing import SplineTransformer
from sklearn.utils.estimator_checks import parametrize_with_checks

from sklego.preprocessing import MonotonicSplineTransformer


@parametrize_with_checks([MonotonicSplineTransformer()])
def test_sklearn_compatible_estimator(estimator, check):
check(estimator)


@pytest.mark.parametrize("n_knots", [3, 5])
@pytest.mark.parametrize("degree", [3, 5])
@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 @@ -33,13 +37,8 @@ def test_monotonic_spline_transformer(n_knots, degree, knots):
# 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
]
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()
assert np.logical_or(np.greater_equal(differences, 0), np.isclose(differences, 0)).all()

0 comments on commit 0ab9442

Please sign in to comment.