Step 3. Nearest neighbor

•      Searches for k-neighbors closest to the target instance

•      Does not need to build a model

Code:

double [] targetvals={35,250,1};

Instance target=new Instance(1,targetvals);

LinearNNSearch kNN=new LinearNNSearch(dataset);

Instances neighbors=kNN.kNearestNeighbours(target, 3);

double [] distances=kNN.getDistances();

 

Try it out:

•      The code is in file ‘WekaKNN.java’

•      Input file ‘GoldDataset.txt’

•      Target instance to classify (age=35,income=250) (hardcoded)

•      K=3

You should get the following output.

 

This is all we need to write our first recommender.