# Code from Chapter 7 of Machine Learning: An Algorithmic Perspective (2nd Edition)
# by Stephen Marsland (http://stephenmonika.net)
# You are free to use, change, or redistribute the code in any way you wish for
# non-commercial purposes, but please maintain the name of the original author.
# This code comes with no warranty of any kind.
# Stephen Marsland, 2008, 2014
import pylab as pl
import numpy as np
# A k-Nearest Neighbour smoother, with three different kernels
# Example is the Ruapehu dataset
def knnSmoother(k,data,testpoints,kernel):
outputs = np.zeros(len(testpoints))
for i in range(len(testpoints)):
distances = (data[:,0]-testpoints[i])
if kernel=='NN':
indices = np.argsort(distances**2,axis=0)
outputs[i] = 1./k * np.sum(data[indices[:k],1])
elif kernel=='Epan':
Klambda = 0.75*(1 - distances**2/k**2)
where = (np.abs(distances)