From 1d4ad6d7395ba325d924d9b9a070d5a787998c46 Mon Sep 17 00:00:00 2001 From: luizfernandolj Date: Wed, 11 Nov 2026 15:34:19 -0300 Subject: [PATCH] exporta o PWKCLF e o faz um estimador do scikit-learn Dois buracos que, juntos, impediam montar "PWK com ajuste de contagem" a partir das pecas que a propria biblioteca ja traz. O PWKCLF nao estava no __init__ de mlquantify.neighbors, que exportava so o PWK: o classificador sobre o qual o metodo e construido nao fazia parte da API publica, e so dava para chega-lo importando o modulo privado. E ele nao implementava get_params, entao sklearn.base.clone o recusava. Com isso ele nao podia ser passado a nenhum quantificador desta biblioteca que valide cruzado -- o ACC clona o estimador --, que e exatamente a combinacao necessaria para obter o PWK ajustado. O conserto do segundo e a conformidade com a convencao do scikit-learn: herdar de ClassifierMixin e BaseEstimator, guardar no __init__ todos os oito argumentos do construtor com o mesmo nome, e construir o NearestNeighbors no fit, nao no __init__ -- um estimador nao deve fazer trabalho antes do fit, e o numero de vizinhos depende do conjunto de treino quando ele e menor que n_neighbors. O caso n < k continua tratado, agora por min(n_neighbors, n) em vez de set_params depois de construido; o resultado e o mesmo. Nenhum outro ponto da biblioteca ou dos testes toca em .nbrs, entao mover a construcao nao alcanca mais ninguem. Quatro testes novos, conferidos nos dois codigos: falham os quatro sem o conserto e passam os quatro com ele. O do clone foi isolado do da exportacao importando a classe do modulo privado, para provar que o get_params esta sendo exercido e nao so o __init__. Co-Authored-By: Claude Opus 5 --- mlquantify/neighbors/__init__.py | 5 +- mlquantify/neighbors/_classification.py | 36 +++++++++----- tests/test_neighbors.py | 64 +++++++++++++++++++++++++ 3 files changed, 92 insertions(+), 13 deletions(-) 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)