Class NearestNeighborClassifier
- java.lang.Object
-
- ai.libs.jaicore.ml.classification.singlelabel.timeseries.learner.ASimplifiedTSClassifier<java.lang.Integer>
-
- ai.libs.jaicore.ml.classification.singlelabel.timeseries.learner.neighbors.NearestNeighborClassifier
-
public class NearestNeighborClassifier extends ASimplifiedTSClassifier<java.lang.Integer>
K-Nearest-Neighbor classifier for time series. Given an integerk, a distance measured(ai.libs.jaicore.ml.tsc.distances), a training set of time seriesTRAIN = {(x, y)}and a test time seriesT(or a set of test time series).The set of k nearest neighbors
From the labels of the instances inNNforTis a subset (or equal) ofTRAINwith cardinalityksuch that for all(T, S)withSinTRAIN\NNholdsd(S, T) >= max_{T' in NN} d(S, T').NNthe label forTis aggregated, e.g. via majority vote.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classNearestNeighborClassifier.VoteTypeVotes 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.NearestNeighborComparatornearestNeighborComparatorSingleton comparator instance for the nearest neighbor priority queues, used for the nearest neighbor calculation.protected int[]targetsTarget values for the instances.protected double[][]timestampsTimestamp matrix containing the timestamps of the instances.protected double[][]valuesValue matrix containing the time series instances.-
Fields inherited from class ai.libs.jaicore.ml.classification.singlelabel.timeseries.learner.ASimplifiedTSClassifier
classMapper, trained
-
-
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 intcalculatePrediction(double[] testInstance)Calculates predicition on a single test instance.org.api4.java.common.metric.IDistanceMetricgetDistanceMeasure()Getter for the distance measure.intgetK()Getter for the k value, @see #k.NearestNeighborLearningAlgorithmgetLearningAlgorithm(TimeSeriesDataset2 dataset)NearestNeighborClassifier.VoteTypegetVoteType()Getter for the vote type.java.lang.Integerpredict(double[] univInstance)Predicts on univariate instance.java.util.List<java.lang.Integer>predict(TimeSeriesDataset2 dataset)Predicts on a dataset.protected voidsetTargets(int[] targets)Sets the targets.protected voidsetTimestamps(double[][] timestamps)Sets the timestamps.protected voidsetValues(double[][] values)Sets the value matrix.protected intvote(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 intvoteMajority(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 intvoteWeightedProportionalToDistance(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 intvoteWeightedStepwise(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 ai.libs.jaicore.ml.classification.singlelabel.timeseries.learner.ASimplifiedTSClassifier
checkWhetherPredictionIsPossible, getClassMapper, isTrained, predict, setClassMapper, train
-
-
-
-
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.PredictionExceptionPredicts on univariate instance.- Specified by:
predictin classASimplifiedTSClassifier<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:
predictin classASimplifiedTSClassifier<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
-
getVoteType
public NearestNeighborClassifier.VoteType getVoteType()
Getter for the vote type. @see #voteType.- Returns:
- The vote type.
-
getDistanceMeasure
public org.api4.java.common.metric.IDistanceMetric getDistanceMeasure()
Getter for the distance measure. @see #distanceMeasure.- Returns:
-
getLearningAlgorithm
public NearestNeighborLearningAlgorithm getLearningAlgorithm(TimeSeriesDataset2 dataset)
- Specified by:
getLearningAlgorithmin classASimplifiedTSClassifier<java.lang.Integer>
-
-