package org.neuroph.nnet.learning.knn;
import java.util.List;
import org.neuroph.nnet.learning.kmeans.KVector;
/**
* for given vector
* calculate distances to all vectors from list
* and finds minimum vector
* or sort vectors by distance and seelct
*
* @author zoran
*/
public class KNearestNeighbour {
private List dataSet;
/**
* http://en.wikipedia.org/wiki/Selection_algorithm
* @param vector
* @param k
* @return
*/
public KVector[] getKNearestNeighbours( KVector vector, int k ) {
KVector[] nearestNeighbours = new KVector[k];
// calculate distances for entire dataset
for(KVector otherVector : dataSet) {
double distance = vector.distanceFrom(otherVector);
otherVector.setDistance(distance);
}
for(int i=0; i getDataSet() {
return dataSet;
}
public void setDataSet(List dataSet) {
this.dataSet = dataSet;
}
}
Ads help maintain this website.