mlpack.approx_kfn

approx_kfn(...)Approximate furthest neighbor search

>>> from mlpack import approx_kfn

This program implements two strategies for furthest neighbor search. These strategies are:

- The 'qdafn' algorithm from "Approximate Furthest Neighbor in High Dimensions" by R. Pagh, F. Silvestri, J. Sivertsen, and M. Skala, in Similarity Search and Applications 2015 (SISAP).

- The 'DrusillaSelect' algorithm from "Fast approximate furthest neighbors with data-dependent candidate selection", by R.R. Curtin and A.B. Gardner, in Similarity Search and Applications 2016 (SISAP).

These two strategies give approximate results for the furthest neighbor search problem and can be used as fast replacements for other furthest neighbor techniques such as those found in the mlpack_kfn program. Note that typically, the 'ds' algorithm requires far fewer tables and projections than the 'qdafn' algorithm.

Specify a reference set (set to search in) with 'reference', specify a query set with 'query', and specify algorithm parameters with 'num_tables' and 'num_projections' (or don't and defaults will be used). The algorithm to be used (either 'ds'---the default---or 'qdafn') may be specified with 'algorithm'. Also specify the number of neighbors to search for with 'k'.

If no query set is specified, the reference set will be used as the query set. The 'output_model' output parameter may be used to store the built model, and an input model may be loaded instead of specifying a reference set with the 'input_model' option.

Results for each query point can be stored with the 'neighbors' and 'distances' output parameters. Each row of these output matrices holds the k distances or neighbor indices for each query point.

For example, to find the 5 approximate furthest neighbors with 'reference_set' as the reference set and 'query_set' as the query set using DrusillaSelect, storing the furthest neighbor indices to 'neighbors' and the furthest neighbor distances to 'distances', one could call

>>> approx_kfn(query=query_set, reference=reference_set, k=5, algorithm='ds')

>>> neighbors = output['neighbors']

>>> distances = output['distances']

and to perform approximate all-furthest-neighbors search with k=1 on the set 'data' storing only the furthest neighbor distances to 'distances', one could call

>>> approx_kfn(reference=reference_set, k=1)

>>> distances = output['distances']

A trained model can be re-used. If a model has been previously saved to 'model', then we may find 3 approximate furthest neighbors on a query set 'new_query_set' using that model and store the furthest neighbor indices into 'neighbors' by calling

>>> approx_kfn(input_model=model, query=new_query_set, k=3)

>>> neighbors = output['neighbors']

## input options

- algorithm (string): Algorithm to use: 'ds' or 'qdafn'. Default value ds.
- calculate_error (bool): If set, calculate the average distance error for the first furthest neighbor only.
- copy_all_inputs (bool): If specified, all input parameters will be deep copied before the method is run. This is useful for debugging problems where the input parameters are being modified by the algorithm, but can slow down the code.
- exact_distances (numpy matrix or arraylike, float dtype): Matrix containing exact distances to furthest neighbors; this can be used to avoid explicit calculation when --calculate_error is set.
- input_model (mlpack.ApproxKFNModelType): File containing input model.
- k (int): Number of furthest neighbors to search for. Default value 0.
- num_projections (int): Number of projections to use in each hash table. Default value 5.
- num_tables (int): Number of hash tables to use. Default value 5.
- query (numpy matrix or arraylike, float dtype): Matrix containing query points.
- reference (numpy matrix or arraylike, float dtype): Matrix containing the reference dataset.
- verbose (bool): Display informational messages and the full list of parameters and timers at the end of execution.

## output options

The return value from the binding is a dict containing the following elements:

- distances (numpy matrix, float dtype): Matrix to save furthest neighbor distances to.
- neighbors (numpy matrix, int dtype): Matrix to save neighbor indices to.
- output_model (mlpack.ApproxKFNModelType): File to save output model to.