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.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  NearestNeighborClassifier.VoteType
      Votes types that describe how to aggregate the prediciton for a test instance on its nearest neighbors found.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected static ai.libs.jaicore.ml.classification.singlelabel.timeseries.learner.neighbors.NearestNeighborClassifier.NearestNeighborComparator nearestNeighborComparator
      Singleton comparator instance for the nearest neighbor priority queues, used for the nearest neighbor calculation.
      protected int[] targets
      Target values for the instances.
      protected double[][] timestamps
      Timestamp matrix containing the timestamps of the instances.
      protected double[][] values
      Value matrix containing the time series instances.
    • Constructor Summary

      Constructors 
      Constructor Description
      NearestNeighborClassifier​(int k, org.api4.java.common.metric.IDistanceMetric distanceMeasure)
      Creates a k nearest neighbor classifier using majority vote.
      NearestNeighborClassifier​(int k, org.api4.java.common.metric.IDistanceMetric distanceMeasure, NearestNeighborClassifier.VoteType voteType)
      Creates a k nearest neighbor classifier.
      NearestNeighborClassifier​(org.api4.java.common.metric.IDistanceMetric distanceMeasure)
      Creates a 1 nearest neighbor classifier using majority vote.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      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.
      protected int calculatePrediction​(double[] testInstance)
      Calculates predicition on a single test instance.
      org.api4.java.common.metric.IDistanceMetric getDistanceMeasure()
      Getter for the distance measure.
      int getK()
      Getter for the k value, @see #k.
      NearestNeighborLearningAlgorithm getLearningAlgorithm​(TimeSeriesDataset2 dataset)  
      NearestNeighborClassifier.VoteType getVoteType()
      Getter for the vote type.
      java.lang.Integer predict​(double[] univInstance)
      Predicts on univariate instance.
      java.util.List<java.lang.Integer> predict​(TimeSeriesDataset2 dataset)
      Predicts on a dataset.
      protected void setTargets​(int[] targets)
      Sets the targets.
      protected void setTimestamps​(double[][] timestamps)
      Sets the timestamps.
      protected void setValues​(double[][] values)
      Sets the value matrix.
      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.
      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.
      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.
      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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • nearestNeighborComparator

        protected static final ai.libs.jaicore.ml.classification.singlelabel.timeseries.learner.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,
                                         org.api4.java.common.metric.IDistanceMetric 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,
                                         org.api4.java.common.metric.IDistanceMetric 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​(org.api4.java.common.metric.IDistanceMetric 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 org.api4.java.ai.ml.core.exception.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:
        org.api4.java.ai.ml.core.exception.PredictionException - If something fails during the prediction process.
      • predict

        public java.util.List<java.lang.Integer> predict​(TimeSeriesDataset2 dataset)
                                                  throws org.api4.java.ai.ml.core.exception.PredictionException
        Predicts on a dataset.
        Specified by:
        predict in class ASimplifiedTSClassifier<java.lang.Integer>
        Parameters:
        dataset - The dataset.
        Returns:
        List of class predicitons for each instance of the dataset.
        Throws:
        org.api4.java.ai.ml.core.exception.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 org.api4.java.common.metric.IDistanceMetric getDistanceMeasure()
        Getter for the distance measure. @see #distanceMeasure.
        Returns: