Nearest Neighbors in Python given the distance matrix
machine-learning, python, scikit-learn, scipy
Solution
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.
Problem
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.