public class NearestNeighborClassifier extends ASimplifiedTSClassifier<java.lang.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').
NN the label for T is aggregated, e.g. via majority vote.| Modifier and Type | Class and Description |
|---|---|
static class |
NearestNeighborClassifier.VoteType
Votes types that describe how to aggregate the prediciton for a test instance on its nearest neighbors found.
|
| Modifier and Type | Field and Description |
|---|---|
protected static 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.
|
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.
|
classMapper, trained| Constructor and Description |
|---|
NearestNeighborClassifier(int k,
ITimeSeriesDistance distanceMeasure)
Creates a k nearest neighbor classifier using majority vote.
|
NearestNeighborClassifier(int k,
ITimeSeriesDistance distanceMeasure,
NearestNeighborClassifier.VoteType voteType)
Creates a k nearest neighbor classifier.
|
NearestNeighborClassifier(ITimeSeriesDistance distanceMeasure)
Creates a 1 nearest neighbor classifier using majority vote.
|
| Modifier and Type | Method and 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.
|
ITimeSeriesDistance |
getDistanceMeasure()
Getter for the distance measure.
|
int |
getK()
Getter for the k value, @see #k.
|
NearestNeighborLearningAlgorithm |
getLearningAlgorithm(TimeSeriesDataset 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(TimeSeriesDataset 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.
|
checkWhetherPredictionIsPossible, getClassMapper, isTrained, predict, setClassMapper, trainprotected static final ai.libs.jaicore.ml.tsc.classifier.neighbors.NearestNeighborClassifier.NearestNeighborComparator nearestNeighborComparator
protected double[][] values
protected double[][] timestamps
protected int[] targets
public NearestNeighborClassifier(int k,
ITimeSeriesDistance distanceMeasure,
NearestNeighborClassifier.VoteType voteType)
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.public NearestNeighborClassifier(int k,
ITimeSeriesDistance distanceMeasure)
k - The number of nearest neighbors.distanceMeasure - Distance measure for calculating the distances between every pair of train and test instances.public NearestNeighborClassifier(ITimeSeriesDistance distanceMeasure)
distanceMeasure - Distance measure for calculating the distances between every pair of train and test instances.public java.lang.Integer predict(double[] univInstance)
throws PredictionException
predict in class ASimplifiedTSClassifier<java.lang.Integer>univInstance - The univariate instance.PredictionException - If something fails during the prediction process.public java.util.List<java.lang.Integer> predict(TimeSeriesDataset dataset) throws PredictionException
predict in class ASimplifiedTSClassifier<java.lang.Integer>dataset - The dataset.PredictionException - If something fails during the prediction processprotected int calculatePrediction(double[] testInstance)
testInstance - The test instance (not null assured within class).protected java.util.PriorityQueue<ai.libs.jaicore.basic.sets.Pair<java.lang.Integer,java.lang.Double>> calculateNearestNeigbors(double[] testInstance)
testInstance - The time series to determine the k nearest neighbors for.protected int vote(java.util.PriorityQueue<ai.libs.jaicore.basic.sets.Pair<java.lang.Integer,java.lang.Double>> nearestNeighbors)
nearestNeighbors - Priority queue of (class, distance)-pairs for nearest neigbors, sorted by distance ascending. (Not null assured within class)protected int voteWeightedStepwise(java.util.PriorityQueue<ai.libs.jaicore.basic.sets.Pair<java.lang.Integer,java.lang.Double>> nearestNeighbors)
nearestNeighbors - Priority queue of (class, distance)-pairs for nearest neigbors, sorted by distance ascending. (Not null assured within class)protected int voteWeightedProportionalToDistance(java.util.PriorityQueue<ai.libs.jaicore.basic.sets.Pair<java.lang.Integer,java.lang.Double>> nearestNeighbors)
nearestNeighbors - Priority queue of (class, distance)-pairs for nearest neigbors, sorted by distance ascending. (Not null assured within class)protected int voteMajority(java.util.PriorityQueue<ai.libs.jaicore.basic.sets.Pair<java.lang.Integer,java.lang.Double>> nearestNeighbors)
nearestNeighbors - Priority queue of (class, distance)-pairs for nearest neigbors, sorted by distance ascending. (Not null assured within class)protected void setValues(double[][] values)
values - protected void setTimestamps(double[][] timestamps)
timestamps - protected void setTargets(int[] targets)
targets - public int getK()
public NearestNeighborClassifier.VoteType getVoteType()
public ITimeSeriesDistance getDistanceMeasure()
public NearestNeighborLearningAlgorithm getLearningAlgorithm(TimeSeriesDataset dataset)
getLearningAlgorithm in class ASimplifiedTSClassifier<java.lang.Integer>