query abstract method

Iterable<Neighbour> query(
  1. Vector point,
  2. int k,
  3. int searchRadius, {
  4. Distance distance = Distance.euclidean,
})

Accepts a point and finds it's k nearest neighbours using distance type. The search is performed along all bins in searchRadius (in bits) from the point's bin index.

For example, if searchRadius is 2, and the point's bin index is 9 (1001 in binary), it means that all bins with indices in binary:

  • 0001, 1101, 1011, 1000 (1 altered bit on 1st, 2nd, 3rd and 4th position)
  • 0101, 0010, 0110, 0000, 1100, 1011 (2 altered bits on positions (0,1), (0, 2), (1,2), (0,3), (1,3) and (2,3))

will be examined (if such indices exists of course).

The greater searchRadius is, the more bins will be examined by the algorithm.

A neighbour is represented by an index in the points matrix and the distance between the neighbour and the query point

Example:

import 'package:ml_algo/ml_algo.dart';
import 'package:ml_dataframe/ml_dataframe.dart';
import 'package:ml_linalg/vector.dart';

void main() {
  final data = DataFrame([
    [10, 20, 30],
    [11, 19, 31],
    [19, 43, 20],
    [89, 97, 11],
    [12, 32, 10],
  ], headerExists: false);
  final digitCapacity = 3;

  final searcher = RandomBinaryProjectionSearcher(data, digitCapacity, seed: 4);
  final point = Vector.fromList([11, 19, 31]);

  final k = 3; // we will search for 3 nearest neighbour
  final searchRadius = 3;

  final neighbours = searcher.query(point, k, searchRadius);

  print(neighbours);
  // ((Index: 1, Distance: 0.0), (Index: 0, Distance: 1.7320508075688772), (Index: 4, Distance: 24.71841418861655))
  // To access a neighbour, refer to `searcher.points` by the neighbour index
}

Implementation

Iterable<Neighbour> query(Vector point, int k, int searchRadius,
    {Distance distance = Distance.euclidean});