Class KNearestNeighbours

  • All Implemented Interfaces:
    Classifier, Predictor

    public final class KNearestNeighbours
    extends AbstractKNearestNeighbours
    K nearest neighbour classification algorithm that is seeded with a "database" of known examples and predicts based on the k-nearest neighbours majority vote for a class. A KD tree is used internally to speedup the searches, thus the distance metric is restricted to the EuclidianDistance.
    TODO maybe we can add a sampling facility for larger data.
    • Constructor Detail

      • KNearestNeighbours

        public KNearestNeighbours​(int numOutcomes,
                                  int k)
        Constructs a new knn classifier.
        Parameters:
        numOutcomes - the number of different outcomes that can be predicted.
        k - the number of neighbours to analyse to get a prediction (it does so by majority voting).
    • Method Detail

      • train

        public void train​(java.lang.Iterable<de.jungblut.math.DoubleVector> features,
                          java.lang.Iterable<de.jungblut.math.DoubleVector> outcome)
        Description copied from interface: Classifier
        Trains this classifier with the given features and the outcome. This is the streaming method for training, it takes parallel iterables.
        Specified by:
        train in interface Classifier
        Overrides:
        train in class AbstractClassifier
        outcome - the outcome must have classes labeled as doubles. E.G. in the binary case you have a single element and decide between 0d and 1d. In higher dimensional cases you have each of these single elements mapped to a dimension.
      • getNearestNeighbours

        protected java.util.List<de.jungblut.jrpt.VectorDistanceTuple<de.jungblut.math.DoubleVector>> getNearestNeighbours​(de.jungblut.math.DoubleVector feature,
                                                                                                                           int k)
        Description copied from class: AbstractKNearestNeighbours
        Find the k nearest neighbours for the given feature.
        Specified by:
        getNearestNeighbours in class AbstractKNearestNeighbours
        Parameters:
        feature - the feature to find neighbours for.
        k - the number of neighbours to find.
        Returns:
        a list of VectorDistanceTuple's that contain the outcome of the retrieved vectors.