Skip to content

Commit

Permalink
Relax check_X_y for DecayEstimator (#580)
Browse files Browse the repository at this point in the history
* blanding check_X_y for decay estimator

* docs

* make check_X_y optional

* variable naming

---------

Co-authored-by: vincent d warmerdam <vincentwarmerdam@gmail.com>
  • Loading branch information
FBruzzesi and koaning authored Oct 13, 2023
1 parent d5ea509 commit f377999
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
29 changes: 23 additions & 6 deletions sklego/meta/decay_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,27 @@ class DecayEstimator(BaseEstimator):
This meta estimator will only work for estimators that have a
"sample_weights" argument in their `.fit()` method.
The `fit` method computes the weights to pass to the estimator.
.. warning:: By default all the checks on the inputs `X` and `y` are delegated to the wrapped estimator.
To change such behaviour, set `check_input` to `True`.
Remark that if the check is skipped, then `y` should have a `shape` attrbute, which is
used to extract the number of samples in training data, and compute the weights.
The DecayEstimator will use exponential decay to weight the parameters.
w_{t-1} = decay * w_{t}
"""

def __init__(self, model, decay: float = 0.999, decay_func="exponential"):
def __init__(
self, model, decay: float = 0.999, decay_func="exponential", check_input=False
):
self.model = model
self.decay = decay
self.decay_func = decay_func
self.check_input = check_input

def _is_classifier(self):
return any(
Expand All @@ -40,12 +52,17 @@ def fit(self, X, y):
"""
Fit the data after adapting the same weight.
:param X: array-like, shape=(n_columns, n_samples,) training data.
:param y: array-like, shape=(n_samples,) training data.
:param X: array-like, shape=(n_samples, n_features,) training data.
:param y: array-like, shape=(n_samples,) target values.
:return: Returns an instance of self.
"""
X, y = check_X_y(X, y, estimator=self, dtype=FLOAT_DTYPES)
self.weights_ = np.cumprod(np.ones(X.shape[0]) * self.decay)[::-1]

if self.check_input:
X, y = check_X_y(
X, y, estimator=self, dtype=FLOAT_DTYPES, ensure_min_features=0
)

self.weights_ = np.cumprod(np.ones(y.shape[0]) * self.decay)[::-1]
self.estimator_ = clone(self.model)
try:
self.estimator_.fit(X, y, sample_weight=self.weights_)
Expand All @@ -62,7 +79,7 @@ def predict(self, X):
"""
Predict new data.
:param X: array-like, shape=(n_columns, n_samples,) training data.
:param X: array-like, shape=(n_samples, n_features,) data to predict.
:return: array, shape=(n_samples,) the predicted data
"""
if self._is_classifier():
Expand Down
4 changes: 2 additions & 2 deletions tests/test_meta/test_decay_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@

@pytest.mark.parametrize("test_fn", flatten([general_checks, regressor_checks]))
def test_estimator_checks_regression(test_fn):
trf = DecayEstimator(LinearRegression())
trf = DecayEstimator(LinearRegression(), check_input=True)
test_fn(DecayEstimator.__name__, trf)


@pytest.mark.parametrize("test_fn", flatten([general_checks, classifier_checks]))
def test_estimator_checks_classification(test_fn):
trf = DecayEstimator(LogisticRegression(solver="lbfgs"))
trf = DecayEstimator(LogisticRegression(solver="lbfgs"), check_input=True)
test_fn(DecayEstimator.__name__, trf)


Expand Down

0 comments on commit f377999

Please sign in to comment.