Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor transformed estimators #52

Merged
merged 10 commits into from
Sep 19, 2023
Merged

Conversation

aazuspan
Copy link
Contributor

This would resolve #51 by:

  • Refactoring the TransformedKNeighborsRegressor into an abstract class
  • Moving transformer instantiation out of the estimator fit methods and into an abstract _get_transformer method
  • Moving transformer fitting out of the estimator fit methods and into the TransformedKNeighborsRegressor._set_fitted_transformer method to reduce duplication
  • Creating a YFitMixin to handle transformer fitting for GNN and MSN

This also:

  • Renames the transform_ attribute to transformer_ for consistency with the new methods
  • Adds a _validate_data check with force_all_finite=True when fitting all transformed estimators. This was needed by MSN, but also fixed an xfailing estimator check for GNN, which allowed us to drop that from the tags.

@grovduck Sorry about the convoluted commit history... Not sure exactly how, but I accidentally included all of your commits from #50 in this. Once this gets squashed it shouldn't be a problem at least.

grovduck and others added 9 commits September 12, 2023 15:23
This mixin provides two methods (`predict_independent` and
`score_independent`) that don't include the query point itself when
finding nearest neighbors.  These methods are similar in functionality
to calling `KNeighborsRegressor.kneighbors()` without passing `X`.  In
this way, only "independent" neighbors are used in calculating
predictions and scores.

This PR is intended to fix #45.
* Replace IndependentPredictionMixin functions with a fit function that
  sets estimator attributes
* Change tests to compare against estimator attributes for
  independent_prediction_ and independent_score_
* Clean up variable names used in tests
* Simplify logic in test_predict for choosing the expected prediction
  data
Previously, the core estimators were using three mixins
(`KNeighborsDFIndexCrosswalkMixin`, `TransformedKNeighborsMixin`,
`IndependentPredictionMixin`) and the core
`sklearn.neighbors.KNeighborsRegressor` as superclasses.  This commit
introduces a new subclass (`_KNeighborsRegressor`) that inherits from
`KNeighborsDFIndexCrosswalkMixin` (renamed to `DFIndexCrosswalkMixin`),
`IndependentPredictionMixin`, and
`sklearn.neighbors.KNeighborsRegressor.  Additionally, the functionality
in `TransformedKNeighborsMixin` is now presented in a subclass of
`_KNeighborsRegressor` renamed to `_TransformedKNeighborsRegressor`.
All core estimators now either inherit directly from
`_TransformedKNeighborsRegressor` or `_KNeighborsRegressor`.
@aazuspan aazuspan added refactor Code cleanup without changing functionality estimator Related to one or more estimators labels Sep 18, 2023
@aazuspan aazuspan added this to the Core Estimators milestone Sep 18, 2023
@aazuspan aazuspan self-assigned this Sep 18, 2023
@aazuspan aazuspan linked an issue Sep 18, 2023 that may be closed by this pull request
Copy link
Member

@grovduck grovduck left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aazuspan, all looks great to me. Exactly as you laid it out ...

src/sknnr/_gnn.py Outdated Show resolved Hide resolved
src/sknnr/_gnn.py Show resolved Hide resolved
@aazuspan
Copy link
Contributor Author

Thanks @grovduck, merging!

@aazuspan aazuspan merged commit 96a4230 into fb_add_estimators Sep 19, 2023
10 checks passed
@aazuspan aazuspan deleted the refactor_transformed branch September 19, 2023 19:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
estimator Related to one or more estimators refactor Code cleanup without changing functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactor transformed estimators
2 participants