First page Back Continue Last page Overview Graphics

Determining the Neighbors

To determine the similarity between two instances, we need a distance function. In our example, the Euclidean distance is ideal:

def distance(instance1, instance2):

# just in case, if the instances are lists or tuples:

instance1 = np.array(instance1)

instance2 = np.array(instance2)

return np.linalg.norm(instance1 - instance2)

print(distance([3, 5], [1, 1]))

print(distance(learnset_data[3], learnset_data[44]))

4.472135955

3.41906419946