Package de.jungblut.classification.knn
Class AbstractKNearestNeighbours
- java.lang.Object
-
- de.jungblut.classification.AbstractPredictor
-
- de.jungblut.classification.AbstractClassifier
-
- de.jungblut.classification.knn.AbstractKNearestNeighbours
-
- All Implemented Interfaces:
Classifier,Predictor
- Direct Known Subclasses:
KNearestNeighbours,SparseKNearestNeighbours
public abstract class AbstractKNearestNeighbours extends AbstractClassifier
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.
-
-
Field Summary
Fields Modifier and Type Field Description protected intkprotected intnumOutcomes
-
Constructor Summary
Constructors Constructor Description AbstractKNearestNeighbours(int numOutcomes, int k)Constructs a new knn classifier.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract 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.de.jungblut.math.DoubleVectorpredict(de.jungblut.math.DoubleVector features)Classifies the given features.de.jungblut.math.DoubleVectorpredictProbability(de.jungblut.math.DoubleVector features)Classifies the given features.-
Methods inherited from class de.jungblut.classification.AbstractClassifier
train, 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
-
-
-
-
Constructor Detail
-
AbstractKNearestNeighbours
public AbstractKNearestNeighbours(int numOutcomes, int k)Constructs a new knn classifier.- Parameters:
numOutcomes- the number of different outcomes that can be predicted.k- the number of neighbours to analyse to get a prediction (it does so by majority voting).
-
-
Method Detail
-
predict
public de.jungblut.math.DoubleVector predict(de.jungblut.math.DoubleVector features)
Description copied from interface:PredictorClassifies the given features.- Returns:
- If the number of outcomes is 2 (binary prediction) the returned vector contains the class id (0 or 1) at the first index. If not, a histogram of the classes that were predicted.
-
predictProbability
public de.jungblut.math.DoubleVector predictProbability(de.jungblut.math.DoubleVector features)
Description copied from interface:PredictorClassifies the given features.- Specified by:
predictProbabilityin interfacePredictor- Overrides:
predictProbabilityin classAbstractPredictor- Returns:
- a vector that returns the probability of all outcomes. The output vector should sum to one.
-
getNearestNeighbours
protected abstract 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.- 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.
-
-