Reputation: 31
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
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
Here is an example of custom metric as well.
Upvotes: 1