diff --git a/mlquantify/neighbors/__init__.py b/mlquantify/neighbors/__init__.py index 172d6858..5d62a8e0 100644 --- a/mlquantify/neighbors/__init__.py +++ b/mlquantify/neighbors/__init__.py @@ -1,5 +1,8 @@ from ._classes import ( PWK, ) +from ._classification import ( + PWKCLF, +) -__all__ = ["PWK"] +__all__ = ["PWK", "PWKCLF"] diff --git a/mlquantify/neighbors/_classification.py b/mlquantify/neighbors/_classification.py index 348c46a7..323d1c7d 100644 --- a/mlquantify/neighbors/_classification.py +++ b/mlquantify/neighbors/_classification.py @@ -1,10 +1,11 @@ import numpy as np +from sklearn.base import BaseEstimator, ClassifierMixin from sklearn.neighbors import NearestNeighbors -class PWKCLF: +class PWKCLF(ClassifierMixin, BaseEstimator): r"""Probabilistic Weighted k-Nearest Neighbor Classifier (PWKCLF). A weighted k-nearest neighbor classifier that assigns class probabilities to @@ -61,16 +62,18 @@ def __init__(self, p=2, metric_params=None, n_jobs=None): + # Only store the constructor arguments here. ``get_params`` reads them + # back by name, and ``sklearn.base.clone`` rebuilds the estimator from + # them -- so every argument must survive as an attribute of the same + # name, and no work may happen before ``fit``. self.alpha = alpha self.n_neighbors = n_neighbors - - self.nbrs = NearestNeighbors(n_neighbors=n_neighbors, - algorithm=algorithm, - leaf_size=leaf_size, - metric=metric, - p=p, - metric_params=metric_params, - n_jobs=n_jobs) + self.algorithm = algorithm + self.metric = metric + self.leaf_size = leaf_size + self.p = p + self.metric_params = metric_params + self.n_jobs = n_jobs self.classes_ = None self.class_to_index = None @@ -95,9 +98,18 @@ def fit(self, X, y): The fitted instance. """ n_samples = X.shape[0] - if n_samples < self.n_neighbors: - self.nbrs.set_params(n_neighbors=n_samples) - + # Built here, not in ``__init__``: a scikit-learn estimator must do no + # work before ``fit``, and the neighbour count depends on the training + # set when it is smaller than ``n_neighbors``. + self.nbrs = NearestNeighbors( + n_neighbors=min(self.n_neighbors, n_samples), + algorithm=self.algorithm, + leaf_size=self.leaf_size, + metric=self.metric, + p=self.p, + metric_params=self.metric_params, + n_jobs=self.n_jobs) + self.y_train = y unique_classes, class_counts = np.unique(y, return_counts=True) diff --git a/tests/test_neighbors.py b/tests/test_neighbors.py index 9c7d8324..b451dd6d 100644 --- a/tests/test_neighbors.py +++ b/tests/test_neighbors.py @@ -39,3 +39,67 @@ def test_pwk_params(binary_dataset): # Test high neighbors count q = PWK(n_neighbors=len(X) - 1).fit(X, y) + + +# -------------------------------------------------------------------------- +# PWKCLF as a scikit-learn estimator +# +# Both of these were broken: ``PWKCLF`` was not exported, and it had no +# ``get_params``, so ``sklearn.base.clone`` refused it. Together that meant a +# user could not build "PWK with count adjustment" out of the two pieces this +# library already ships -- ``ACC`` clones its estimator for cross-validation. +# -------------------------------------------------------------------------- + +def test_pwkclf_is_exported(): + from mlquantify.neighbors import PWKCLF # noqa: F401 + + import mlquantify.neighbors as mod + + assert "PWKCLF" in mod.__all__ + + +def test_pwkclf_get_params_round_trips(): + from sklearn.base import clone + + from mlquantify.neighbors import PWKCLF + + clf = PWKCLF(alpha=2.0, n_neighbors=7, metric="manhattan", leaf_size=17) + params = clf.get_params() + assert params["alpha"] == 2.0 + assert params["n_neighbors"] == 7 + assert params["metric"] == "manhattan" + assert params["leaf_size"] == 17 + + copia = clone(clf) + assert copia is not clf + assert copia.get_params() == params + + +def test_pwkclf_fits_after_clone(binary_dataset): + from sklearn.base import clone + + from mlquantify.neighbors import PWKCLF + + X, y = binary_dataset + clf = clone(PWKCLF(n_neighbors=5)) + clf.fit(X, y) + rotulos = clf.predict(X) + assert rotulos.shape == (X.shape[0],) + assert set(np.unique(rotulos)).issubset(set(np.unique(y))) + + +def test_pwkclf_works_as_estimator_of_acc(binary_dataset): + """O conserto que importa: montar PWK com ajuste de contagem.""" + from mlquantify.counting import ACC + from mlquantify.neighbors import PWKCLF + + X, y = binary_dataset + q = ACC(estimator=PWKCLF(n_neighbors=5), cv=3) + q.fit(X, y) + _assert_valid_prevalence_dict(q.predict(X), n_classes=2) + + +def _assert_valid_prevalence_dict(prevalence, n_classes): + if isinstance(prevalence, dict): + prevalence = np.asarray([prevalence[k] for k in sorted(prevalence)]) + _assert_valid_prevalence(prevalence, n_classes)