1

I would like to change the distance used by KNeighborsClassifier from sklearn. By distance I mean the one in the feature space to see who are the neighbors of a point. More specifically, I want to use the following distance:

d(X1,X2) = 0.1 * |X1[0] - X2[0]| + 0.9*|X1[1] - X2[1]|

Thank you.

JNYC
  • 31
  • 1
  • 5

1 Answers1

1

Just define your custom metric like this:

def mydist(X1, X2):
    return 0.1 * abs(X1[0] - X2[0]) + 0.9*abs(X1[1] - X2[1])

Then initialize your KNeighboursClassifier using the metric parameter like this

clf = KNeighborsClassifier(n_neighbors=3,metric=mydist,)

You can read more about distances available in sklearn and custom distance measures here

Just be sure that according to the official documentation, your custom metric should follow the following properties

  1. Non-negativity: d(x, y) >= 0
  2. Identity: d(x, y) = 0 if and only if x == y
  3. Symmetry: d(x, y) = d(y, x)
  4. Triangle Inequality: d(x, y) + d(y, z) >= d(x, z)

Here is an example of custom metric as well.

Gambit1614
  • 8,547
  • 1
  • 25
  • 51
  • 1
    Note, that this will work, but will be considerably slower. See my answer [here](https://stackoverflow.com/a/50954829/6694255) which also applies to your problem. You may have to consider forking KNN and building your metric in Cython, if speed is relevant to you (or implementing it completely yourself, e.g. using BallTree from sklearn). – Marcus V. Jun 28 '18 at 06:34