title: Sklearn kNN usage with a user defined metric
body: <p>Currently I'm doing a project which may require using a kNN algorithm to find the  top k nearest neighbors for a given point, say P. im using python, sklearn package to do the job, but our predefined metric is not one of those default metrics. so I have to use the user defined metric, from the documents of sklearn, which can be find <a href="http://scikit-learn.org/stable/modules/generated/sklearn.neighbors.KNeighborsClassifier.html#sklearn.neighbors.KNeighborsClassifier.get_params">here</a> and <a href="http://scikit-learn.org/stable/modules/generated/sklearn.neighbors.DistanceMetric.html">here</a>.</p>
  
  <p>It seems that the latest version of sklearn kNN support the user defined metric, but i cant find how to use it:</p>
  
  <pre><code>import sklearn
  from sklearn.neighbors import NearestNeighbors
  import numpy as np
  from sklearn.neighbors import DistanceMetric
  from sklearn.neighbors.ball_tree import BallTree
  BallTree.valid_metrics
  </code></pre>
  
  <p>say i have defined a metric called mydist=max(x-y), then use DistanceMetric.get_metric to make it a DistanceMetric object:</p>
  
  <pre><code>dt=DistanceMetric.get_metric('pyfunc',func=mydist)
  </code></pre>
  
  <p>from the document, the line should looks like this</p>
  
  <pre><code>nbrs = NearestNeighbors(n_neighbors=4, algorithm='auto',metric='pyfunc').fit(A)
  distances, indices = nbrs.kneighbors(A)
  </code></pre>
  
  <p>but where can i put the <code>dt</code> in? Thanks</p>