How to use a self defined Distance Metric in Sklearn
<p>I have written the code to compute distance between two rows of input matrices and plan on running KNeighborsClassifier on it.
How do I use a different distance metric in Sklearn KNeighborsClassifier?</p>

<p><em>For example:</em></p>

<pre><code>def distanceMetric(a, b):
    distance = &lt;some distance&gt;
    return distance
</code></pre>

<p>Also which Classifiers offer the ability to define a new distance function?</p>

<p>Sklearn has a bunch of built in distance metrics. But if you would like to use your own you do the following:</p>

<pre><code>NearestNeighbors(metric='pyfunc', func=distanceMetric)
</code></pre>

<p>Check out the <a href="http://scikit-learn.org/stable/modules/generated/sklearn.neighbors.DistanceMetric.html" rel="nofollow">distance metric page</a> in sklearn for a full list of options. </p>

python machine-learning scikit-learn distance knn