Repository navigation
Export PWKCLF and make it a scikit-learn estimator - #16
Open
luizfernandolj wants to merge 1 commit into
Open
luizfernandolj wants to merge 1 commit into
luizfernandolj wants to merge 1 commit into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two gaps that, together, made it impossible to build PWK with count adjustment out of pieces this library already ships.
What was broken
PWKCLFwas not exported.mlquantify/neighbors/__init__.pypublished onlyPWK, so the classifier the method is built on was not part of the public API — the only way to reach it wasfrom mlquantify.neighbors._classification import PWKCLF.PWKCLFhad noget_params, sosklearn.base.clonerefused it:That rules it out as the estimator of any quantifier in this library that cross-validates —
ACCclones its estimator — which is exactly the combination needed to obtain the adjusted PWK.The fix
Adding
get_paramsalone would not have worked:__init__kept onlyalphaandn_neighborsas attributes, while the other six constructor arguments were folded into aNearestNeighborsand lost, soget_paramswould raiseAttributeError. The fix is conformance with the scikit-learn convention:ClassifierMixin, BaseEstimator;NearestNeighborsinfit, not in__init__— an estimator must do no work beforefit, and the neighbour count depends on the training set when it is smaller thann_neighbors.The
n < kcase is still handled, now bymin(n_neighbors, n)instead ofset_paramsafter 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, theget_paramsround trip throughclone, fitting after aclone, 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, theclonetest was also verified by importing the class from the private module, soget_paramsis exercised and not just the export.🤖 Generated with Claude Code