From 0ab944297602c6c8c506df0be28110483be5729f Mon Sep 17 00:00:00 2001 From: vincent d warmerdam Date: Wed, 6 Nov 2024 14:50:11 +0000 Subject: [PATCH] base-tests --- sklego/preprocessing/monotonicspline.py | 4 ++++ tests/test_preprocessing/test_monospline.py | 19 +++++++++---------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/sklego/preprocessing/monotonicspline.py b/sklego/preprocessing/monotonicspline.py index 39638d11..5c4c3761 100644 --- a/sklego/preprocessing/monotonicspline.py +++ b/sklego/preprocessing/monotonicspline.py @@ -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): @@ -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( diff --git a/tests/test_preprocessing/test_monospline.py b/tests/test_preprocessing/test_monospline.py index 85533f84..fe567b3a 100644 --- a/tests/test_preprocessing/test_monospline.py +++ b/tests/test_preprocessing/test_monospline.py @@ -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) @@ -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()