Package de.jungblut.classification.knn
Class KNearestNeighbours
- java.lang.Object
-
- de.jungblut.classification.AbstractPredictor
-
- de.jungblut.classification.AbstractClassifier
-
- de.jungblut.classification.knn.AbstractKNearestNeighbours
-
- de.jungblut.classification.knn.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.
-
-
Field Summary
-
Fields inherited from class de.jungblut.classification.knn.AbstractKNearestNeighbours
k, numOutcomes
-
-
Constructor Summary
Constructors Constructor Description KNearestNeighbours(int numOutcomes, int k)Constructs a new knn classifier.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected java.util.List<de.jungblut.jrpt.VectorDistanceTuple<de.jungblut.math.DoubleVector>>getNearestNeighbours(de.jungblut.math.DoubleVector feature, int k)Find the k nearest neighbours for the given feature.voidtrain(java.lang.Iterable<de.jungblut.math.DoubleVector> features, java.lang.Iterable<de.jungblut.math.DoubleVector> outcome)Trains this classifier with the given features and the outcome.-
Methods inherited from class de.jungblut.classification.knn.AbstractKNearestNeighbours
predict, predictProbability
-
Methods inherited from class de.jungblut.classification.AbstractClassifier
train
-
Methods inherited from class de.jungblut.classification.AbstractPredictor
extractPredictedClass, extractPredictedClass, predictedClass, predictedClass
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface de.jungblut.classification.Predictor
asClassifier, extractPredictedClass, extractPredictedClass, predictedClass, predictedClass
-
-
-
-
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:ClassifierTrains this classifier with the given features and the outcome. This is the streaming method for training, it takes parallel iterables.- Specified by:
trainin interfaceClassifier- Overrides:
trainin classAbstractClassifieroutcome- 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:AbstractKNearestNeighboursFind the k nearest neighbours for the given feature.- Specified by:
getNearestNeighboursin classAbstractKNearestNeighbours- 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.
-
-