simple 1NN

I'm unable to figure out why my code is not working as expected when I try to run 1NN on entire test data set. Same code works when I use it to predict value of 1 test data set.