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
- 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)
Here is an example of custom metric as well.