Skip to content

Instantly share code, notes, and snippets.

@darkseed
Forked from Newmu/nearest_neighbor.py
Last active August 29, 2015 14:24
Show Gist options
  • Save darkseed/f774646721899087e15c to your computer and use it in GitHub Desktop.
Save darkseed/f774646721899087e15c to your computer and use it in GitHub Desktop.
import numpy as np
from scipy.spatial.distance import cdist
def nearest_neighbor(samples, targets, samples_to_classify, metric='euclidean'):
return targets[np.argmin(cdist(samples_to_classify, samples, metric=metric), axis=1)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment