Sklearn kNN usage with a user defined metric
You pass a metric as metric param, and additional metric arguments as keyword paramethers to NN constructor: >>> 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) NearestNeighbors(algorithm=’ball_tree’, leaf_size=30, metric=”pyfunc”, n_neighbors=4, radius=1.0) … Read more