Range search is a simple machine learning task which aims to find all the neighbors of a point that fall into a certain range of distances. In this setting, we have a query and a reference dataset. Given a certain range, for each point in the query dataset, we wish to know all points in the reference dataset which have distances within that given range to the given query point.
Alternately, if the query and reference datasets are the same, the problem can be stated more simply: for each point in the dataset, we wish to know all points which have distance in the given range to that point.
- a simple command-line executable to run range search
- a simple C++ interface to perform range search
- a generic, extensible, and powerful C++ class (RangeSearch) for complex usage
A list of all the sections this tutorial contains.
- Table of Contents
- The 'mlpack_range_search' command-line executable
- The 'RangeSearch' class
- The extensible 'RangeSearch' class
- Further documentation
mlpack provides an executable,
mlpack_range_search, which can be used to perform range searches quickly and simply from the command-line. This program will perform the range search and place the resulting neighbor index list into one file and their corresponding distances into another file. These files are organized such that the first row corresponds to the neighbors (or distances) of the first query point, and the second row corresponds to the neighbors (or distances) of the second query point, and so forth. The neighbors of a specific point are not arranged in any specific order.
Because a range search may return different numbers of points (including zero), the output file is technically not a valid CSV and may not be loadable by other programs. Therefore, if you need the results in a certain format, it may be better to use the C++ interface to manually export the data in the preferred format.
Below are several examples of simple usage (and the resultant output). The '-v' option is used so that output is given. Further documentation on each individual option can be found by typing
Convenient program timers are given for different parts of the calculation at the bottom of the output, as well as the parameters the simulation was run with. Now, if we look at the output files:
We can see that only point 862 is within distance 0.076 of point 0. We can also see that point 2 has no points within a distance of 0.076 – that line is empty.
The mlpack implementation of range search is a dual-tree algorithm; when -trees are used, the leaf size of the tree can be changed. Depending on the characteristics of the dataset, a larger or smaller leaf size can provide faster computation. The leaf size is modifiable through the command-line interface, as shown below.
Further documentation on options should be found by using the –help option.
The 'RangeSearch' class is an extensible template class which allows a high level of flexibility. However, all of the template arguments have default parameters, allowing a user to simply use 'RangeSearch<>' for simple usage without worrying about the exact necessary template parameters.
The class bears many similarities to the NeighborSearch class; usage generally consists of calling the constructor with one or two datasets, and then calling the 'Search()' method to perform the actual range search.
The 'Search()' method stores the results in two vector-of-vector objects. This is necessary because each query point may have a different number of neighbors in the specified distance range. The structure of those two objects is very similar to the output files –neighbors_file and –distances_file for the CLI interface (see above). A handful of examples of simple usage of the RangeSearch class are given below.
The output of the search is stored in resultingNeighbors and resultingDistances.
This example uses the O(n^2) naive search (not the tree-based search).
Needless to say, naive search can be very slow...
Similar to the NeighborSearch class, the RangeSearch class is very extensible, having the following template arguments:
By choosing different components for each of these template classes, a very arbitrary range searching object can be constructed.
The MetricType policy class allows the range search to take place in any arbitrary metric space. The mlpack::metric::LMetric class is a good example implementation. A MetricType class must provide the following functions:
Internally, the RangeSearch class keeps an instantiated MetricType class (which can be given in the constructor). This is useful for a metric like the Mahalanobis distance (mlpack::metric::MahalanobisDistance), which must store state (the covariance matrix). Therefore, you can write a non-static MetricType class and use it seamlessly with RangeSearch.
The MatType template parameter specifies the type of data matrix used. This type must implement the same operations as an Armadillo matrix, and so standard choices are
The RangeSearch class also allows a custom tree to be used. The TreeType policy is also used elsewhere in mlpack and is documented more thoroughly here.
Typical choices might include mlpack::tree::KDTree (the default), mlpack::tree::BallTree, mlpack::tree::RTree, mlpack::tree::RStarTree, or mlpack::tree::StandardCoverTree. Below is an example that uses the RangeSearch class with an R-tree:
For further information on trees, including how to write your own tree for use with RangeSearch and other mlpack methods, see the TreeType policy documentation.
For further documentation on the RangeSearch class, consult the complete API documentation.
Generated by 1.8.13