Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nearest Neighbors in Python given the distance matrix

I have to apply Nearest Neighbors in Python, and I am looking ad the scikit-learn and the scipy libraries, which both require the data as input, then will compute the distances and apply the algorithm.

In my case I had to compute a non-conventional distance, therefore I would like to know if there is a way to directly feed the distance matrix.

like image 860
gcedo Avatar asked Feb 10 '14 11:02

gcedo


2 Answers

You'll want to create a DistanceMetric object, supplying your own function as an argument:

metric = sklearn.neighbors.DistanceMetric.get_metric('pyfunc', func=func)

From the docs:

Here func is a function which takes two one-dimensional numpy arrays, and returns a distance. Note that in order to be used within the BallTree, the distance must be a true metric: i.e. it must satisfy the following properties

  • Non-negativity: d(x, y) >= 0
  • Identity: d(x, y) = 0 if and only if x == y
  • Symmetry: d(x, y) = d(y, x)
  • Triangle Inequality: d(x, y) + d(y, z) >= d(x, z)

You can then create your classifier with metric=metric as a keyword argument and it will use this when calculating distances.

like image 163
ford Avatar answered Sep 21 '22 19:09

ford


As said by ford and according to the documentation http://scikit-learn.org/stable/modules/generated/sklearn.neighbors.KNeighborsClassifier.html#sklearn.neighbors.KNeighborsClassifier you should convert your custom distance to a DistanceMetric object and pass it as the metric parameter.

like image 28
tk. Avatar answered Sep 19 '22 19:09

tk.