Skip to content

Export PWKCLF and make it a scikit-learn estimator - #16

Open
luizfernandolj wants to merge 1 commit into
masterfrom
pwkclf-sklearn-estimator
Open

luizfernandolj wants to merge 1 commit into
masterfrom
pwkclf-sklearn-estimator

Conversation

@luizfernandolj

Copy link
Copy Markdown
Collaborator

Two gaps that, together, made it impossible to build PWK with count adjustment out of pieces this library already ships.

What was broken

PWKCLF was not exported. mlquantify/neighbors/__init__.py published only PWK, so the classifier the method is built on was not part of the public API — the only way to reach it was from mlquantify.neighbors._classification import PWKCLF.

PWKCLF had no get_params, so sklearn.base.clone refused it:

TypeError: Cannot clone object '<...PWKCLF object...>': it does not seem to be
a scikit-learn estimator as it does not implement a 'get_params' method.

That rules it out as the estimator of any quantifier in this library that cross-validates — ACC clones its estimator — which is exactly the combination needed to obtain the adjusted PWK.

The fix

Adding get_params alone would not have worked: __init__ kept only alpha and n_neighbors as attributes, while the other six constructor arguments were folded into a NearestNeighbors and lost, so get_params would raise AttributeError. The fix is conformance with the scikit-learn convention:

  • inherit from ClassifierMixin, BaseEstimator;
  • store all eight constructor arguments under their own names;
  • build the NearestNeighbors in fit, not in __init__ — an estimator must do no work before fit, and the neighbour count depends on the training set when it is smaller than n_neighbors.

The n < k case is still handled, now by min(n_neighbors, n) instead of set_params after construction; the result is the same. Nothing else in the library or the tests touches .nbrs, so moving the construction reaches no one else.

Tests

Four new tests in tests/test_neighbors.py: the export, the get_params round trip through clone, fitting after a clone, and the one that matters — ACC(estimator=PWKCLF(...)) fitting and predicting.

They were checked against both code paths: all four fail without this change and pass with it. Since all four would otherwise fail on the same ImportError, which would mask the second fix, the clone test was also verified by importing the class from the private module, so get_params is exercised and not just the export.

🤖 Generated with Claude Code

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 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant