Last active
August 29, 2015 13:57
-
-
Save Newmu/9460823 to your computer and use it in GitHub Desktop.
Given a training dataset as a 2d array where rows are examples and columns are features, values associated with those training examples, and some new examples to be predicted, return the value associated with the closest nearest neighbor using a scipy distance metric.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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