Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,10 @@ target/
.LSOverride

# auto-generated files
skltemplate/_version.py
skltemplate/_version.py

# linters and formatters
.vscode/
.idea/
.mypy_cache/
.ruff_cache/
3 changes: 2 additions & 1 deletion skltemplate/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Authors: scikit-learn-contrib developers
# License: BSD 3 clause

from sklearn import __version__

from ._template import TemplateClassifier, TemplateEstimator, TemplateTransformer
from ._version import __version__

__all__ = [
"TemplateEstimator",
Expand Down
19 changes: 11 additions & 8 deletions skltemplate/_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

# Authors: scikit-learn-contrib developers
# License: BSD 3 clause
# mypy: ignore-errors

import numpy as np
from sklearn.base import BaseEstimator, ClassifierMixin, TransformerMixin, _fit_context
from sklearn.metrics import euclidean_distances
from sklearn.utils.multiclass import check_classification_targets
from sklearn.utils.validation import check_is_fitted
from sklearn.utils.validation import check_is_fitted, validate_data


class TemplateEstimator(BaseEstimator):
Expand Down Expand Up @@ -73,12 +74,14 @@ def fit(self, X, y):
self : object
Returns self.
"""
# `_validate_data` is defined in the `BaseEstimator` class.
# `_validate_data` is defined in the sklearn.utils.validation module.
# It allows to:
# - run different checks on the input data;
# - define some attributes associated to the input data: `n_features_in_` and
# `feature_names_in_`.
X, y = self._validate_data(X, y, accept_sparse=True)

X, y = validate_data(self, X, y, accept_sparse=True)

self.is_fitted_ = True
# `fit` should always return `self`
return self
Expand All @@ -100,7 +103,7 @@ def predict(self, X):
check_is_fitted(self)
# We need to set reset=False because we don't want to overwrite `n_features_in_`
# `feature_names_in_` but only check that the shape is consistent.
X = self._validate_data(X, accept_sparse=True, reset=False)
X = validate_data(self, X, accept_sparse=True, reset=False)
return np.ones(X.shape[0], dtype=np.int64)


Expand Down Expand Up @@ -182,7 +185,7 @@ def fit(self, X, y):
# - run different checks on the input data;
# - define some attributes associated to the input data: `n_features_in_` and
# `feature_names_in_`.
X, y = self._validate_data(X, y)
X, y = validate_data(self, X, y)
# We need to make sure that we have a classification task
check_classification_targets(y)

Expand Down Expand Up @@ -216,7 +219,7 @@ def predict(self, X):
# Input validation
# We need to set reset=False because we don't want to overwrite `n_features_in_`
# `feature_names_in_` but only check that the shape is consistent.
X = self._validate_data(X, reset=False)
X = validate_data(self, X, reset=False)

closest = np.argmin(euclidean_distances(X, self.X_), axis=1)
return self.y_[closest]
Expand Down Expand Up @@ -272,7 +275,7 @@ def fit(self, X, y=None):
self : object
Returns self.
"""
X = self._validate_data(X, accept_sparse=True)
X = validate_data(self, X, accept_sparse=True)

# Return the transformer
return self
Expand All @@ -297,7 +300,7 @@ def transform(self, X):
# Input validation
# We need to set reset=False because we don't want to overwrite `n_features_in_`
# `feature_names_in_` but only check that the shape is consistent.
X = self._validate_data(X, accept_sparse=True, reset=False)
X = validate_data(self, X, accept_sparse=True, reset=False)
return np.sqrt(X)

def _more_tags(self):
Expand Down