JNYC
JNYC

Reputation: 31

KNeighborsClassifier change of distance

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.

Upvotes: 1

Views: 2444

Answers (1)

Gambit1614
Gambit1614

Reputation: 8811

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.

Upvotes: 1

Related Questions