scikit-learn/scikit-learn — pull request #3922 by jnothman (2014-12-01 04:56:07+00:00)
Title: [WIP] allow nearest neighbors algorithm to be an estimator

This works towards allowing an `LSHForest` (see #3894) instance as a valid value for the `algorithm` parameter to nearest neighbors classes (and the `neighbors_algorithm` parameter elsewhere) while minimising extra support code.

The idea is that it should be possible to pass as `algorithm` any estimator that has `fit` and at least one of `kneighbors` or `radius_neighbors` implemented. Until this PR, `KDTree` and `BallTree` instead take the data upon construction (lacking `fit`) and have `query`/`query_radius` with extremely similar interface, instead of using the public API names. Additionally, `query_radius` sends its return values in the reverse order for no apparent reason. This PR thus includes backwards-incompatible changes to the `KDTree` and `BallTree` semi-public APIs, the main issue being not accepting data upon construction.

Another change is that previously `radius_neighbors` would return a 2d array or a 1d array of arrays, depending on whether the number of neighbors within radius for all queries was equal, but only if `algorithm='brute'`. This PR changes it to alway return an array of arrays.

TODO:
[ ] test for safe failure if `KDTree`/`BallTree` methods are called before `fit`
[ ] test returning array of objects from `radius_neighbors`
[ ] rename `_tree` to `_index` or `_storage` or similar
[ ] perhaps factor out brute as another indexing estimator
[ ] allow estimator as value of `algorithm`