Created
November 19, 2012 13:05
-
-
Save rofr/4110536 to your computer and use it in GitHub Desktop.
kNN Classifier
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public double classify(T item) { | |
int numItems = items.size(); | |
// calculated distances in same order as the items list | |
double[] distances = new double[numItems]; | |
//calculate the distance to each instance | |
for (int i = 0; i < numItems; i++) { | |
distances[i] = distanceFunction.calculateDistance(item, items.get(i)); | |
} | |
//Find the k nearest items | |
List<T> nearestItems = getNearestItems(distances); | |
double result = averageFunction.calculateAverage(nearestItems); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment