Skip to content

Commit

Permalink
Finalize estimator tags (#84)
Browse files Browse the repository at this point in the history
- CCA now correctly requires positive targets
- CCorA now correctly requires targets
- All estimators (other than `RawKNNRegressor`) now explicitly 
prohibit sparse data. This is a temporary measure to fix failing 
checks. A closer investigation of sparse support is needed.
- The addition of `target_tags.required` to CCorA triggered an
estimator check `check_requires_y_none` which looks for an
expected error message when passed `y=None`. That error is now
correctly raised by passing y into `_validate_data`. This removed
the need to separately check the y array. Note that we don't mutate
`X` with the validation check as this will strip feature names that
must be passed to `StandardScalerWithDOF`.
  • Loading branch information
aazuspan authored Jan 18, 2025
1 parent 11b649d commit 27b34ab
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/sknnr/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ def kneighbors(
return_dataframe_index=return_dataframe_index,
)

def __sklearn_tags__(self):
tags = super().__sklearn_tags__()
tags.input_tags.sparse = False

return tags


class OrdinationKNeighborsRegressor(TransformedKNeighborsRegressor, ABC):
"""
Expand Down
1 change: 1 addition & 0 deletions src/sknnr/transformers/_cca_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,6 @@ def fit_transform(self, X, y):
def __sklearn_tags__(self):
tags = super().__sklearn_tags__()
tags.target_tags.required = True
tags.target_tags.positive_only = True

return tags
12 changes: 8 additions & 4 deletions src/sknnr/transformers/_ccora_transformer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import numpy as np
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.utils.validation import check_array, check_is_fitted
from sklearn.utils.validation import check_is_fitted

from .._base import _validate_data
from . import ComponentReducerMixin, StandardScalerWithDOF
Expand All @@ -9,10 +8,9 @@

class CCorATransformer(ComponentReducerMixin, TransformerMixin, BaseEstimator):
def fit(self, X, y):
_validate_data(self, X=X, reset=True)
_, y = _validate_data(self, X=X, y=y, reset=True, multi_output=True)
self.scaler_ = StandardScalerWithDOF(ddof=1).fit(X)

y = check_array(y, input_name="Y", ensure_2d=False, dtype=np.float64)
if y.ndim == 1:
y = y.reshape(-1, 1)
y = StandardScalerWithDOF(ddof=1).fit_transform(y)
Expand All @@ -29,3 +27,9 @@ def transform(self, X, y=None):

def fit_transform(self, X, y):
return self.fit(X, y).transform(X)

def __sklearn_tags__(self):
tags = super().__sklearn_tags__()
tags.target_tags.required = True

return tags

0 comments on commit 27b34ab

Please sign in to comment.