Class NearestNeighborClassifier


  • public class NearestNeighborClassifier
    extends ASimplifiedTSClassifier<java.lang.Integer>
    K-Nearest-Neighbor classifier for time series. Given an integer k, a distance measure d (ai.libs.jaicore.ml.tsc.distances), a training set of time series TRAIN = {(x, y)} and a test time series T (or a set of test time series).

    The set of k nearest neighbors NN for T is a subset (or equal) of TRAIN with cardinality k such that for all (T, S) with S in TRAIN\NN holds d(S, T) >= max_{T' in NN} d(S, T').

    From the labels of the instances in NN the label for T is aggregated, e.g. via majority vote.
    • Field Detail

      • nearestNeighborComparator

        protected static final ai.libs.jaicore.ml.tsc.classifier.neighbors.NearestNeighborClassifier.NearestNeighborComparator nearestNeighborComparator
        Singleton comparator instance for the nearest neighbor priority queues, used for the nearest neighbor calculation.
      • values

        protected double[][] values
        Value matrix containing the time series instances. Set by algorithm.
      • timestamps

        protected double[][] timestamps
        Timestamp matrix containing the timestamps of the instances. Set by the algorihm.
      • targets

        protected int[] targets
        Target values for the instances. Set by the algorithm.
    • Constructor Detail

      • NearestNeighborClassifier

        public NearestNeighborClassifier​(int k,
                                         ITimeSeriesDistance distanceMeasure,
                                         NearestNeighborClassifier.VoteType voteType)
        Creates a k nearest neighbor classifier.
        Parameters:
        k - The number of nearest neighbors.
        distanceMeasure - Distance measure for calculating the distances between every pair of train and test instances.
        voteType - Vote type to use to aggregate the the classes of the the k nearest neighbors into a single class prediction.
      • NearestNeighborClassifier

        public NearestNeighborClassifier​(int k,
                                         ITimeSeriesDistance distanceMeasure)
        Creates a k nearest neighbor classifier using majority vote.
        Parameters:
        k - The number of nearest neighbors.
        distanceMeasure - Distance measure for calculating the distances between every pair of train and test instances.
      • NearestNeighborClassifier

        public NearestNeighborClassifier​(ITimeSeriesDistance distanceMeasure)
        Creates a 1 nearest neighbor classifier using majority vote.
        Parameters:
        distanceMeasure - Distance measure for calculating the distances between every pair of train and test instances.
    • Method Detail

      • predict

        public java.lang.Integer predict​(double[] univInstance)
                                  throws PredictionException
        Predicts on univariate instance.
        Specified by:
        predict in class ASimplifiedTSClassifier<java.lang.Integer>
        Parameters:
        univInstance - The univariate instance.
        Returns:
        Class prediction for the instance.
        Throws:
        PredictionException - If something fails during the prediction process.
      • calculatePrediction

        protected int calculatePrediction​(double[] testInstance)
        Calculates predicition on a single test instance.
        Parameters:
        testInstance - The test instance (not null assured within class).
        Returns:
      • calculateNearestNeigbors

        protected java.util.PriorityQueue<ai.libs.jaicore.basic.sets.Pair<java.lang.Integer,​java.lang.Double>> calculateNearestNeigbors​(double[] testInstance)
        Determine the k nearest neighbors for a test instance.
        Parameters:
        testInstance - The time series to determine the k nearest neighbors for.
        Returns:
        Queue of the k nearest neighbors as pairs (class, distance).
      • vote

        protected int vote​(java.util.PriorityQueue<ai.libs.jaicore.basic.sets.Pair<java.lang.Integer,​java.lang.Double>> nearestNeighbors)
        Performs a vote on the nearest neighbors found. Delegates the vote according to the vote type.
        Parameters:
        nearestNeighbors - Priority queue of (class, distance)-pairs for nearest neigbors, sorted by distance ascending. (Not null assured within class)
        Returns:
        Result of the vote, i.e. the predicted class.
      • voteWeightedStepwise

        protected int voteWeightedStepwise​(java.util.PriorityQueue<ai.libs.jaicore.basic.sets.Pair<java.lang.Integer,​java.lang.Double>> nearestNeighbors)
        Performs a vote with stepwise weights 1, 2, .., k on the set nearest neighbors found.
        Parameters:
        nearestNeighbors - Priority queue of (class, distance)-pairs for nearest neigbors, sorted by distance ascending. (Not null assured within class)
        Returns:
        Result of the vote, i.e. the predicted class.
      • voteWeightedProportionalToDistance

        protected int voteWeightedProportionalToDistance​(java.util.PriorityQueue<ai.libs.jaicore.basic.sets.Pair<java.lang.Integer,​java.lang.Double>> nearestNeighbors)
        Performs a vote with weights proportional to the distance on the set nearest neighbors found.
        Parameters:
        nearestNeighbors - Priority queue of (class, distance)-pairs for nearest neigbors, sorted by distance ascending. (Not null assured within class)
        Returns:
        Result of the vote, i.e. the predicted class.
      • voteMajority

        protected int voteMajority​(java.util.PriorityQueue<ai.libs.jaicore.basic.sets.Pair<java.lang.Integer,​java.lang.Double>> nearestNeighbors)
        Performs a majority vote on the set nearest neighbors found.
        Parameters:
        nearestNeighbors - Priority queue of (class, distance)-pairs for nearest neigbors, sorted by distance ascending. (Not null assured within class)
        Returns:
        Result of the vote, i.e. the predicted class.
      • setValues

        protected void setValues​(double[][] values)
        Sets the value matrix.
        Parameters:
        values -
      • setTimestamps

        protected void setTimestamps​(double[][] timestamps)
        Sets the timestamps.
        Parameters:
        timestamps -
      • setTargets

        protected void setTargets​(int[] targets)
        Sets the targets.
        Parameters:
        targets -
      • getK

        public int getK()
        Getter for the k value, @see #k.
        Returns:
        k
      • getDistanceMeasure

        public ITimeSeriesDistance getDistanceMeasure()
        Getter for the distance measure. @see #distanceMeasure.
        Returns: