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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With