def mydist(x, y):
    return np.sum((x - y) ** 2)
X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
nbrs = NearestNeighbors(n_neighbors=4, algorithm='ball_tree', metric=
    'pyfunc', func=mydist)
nbrs.fit(X)
nbrs.kneighbors(X)
Sklearn kNN usage with a user defined metric