Sklearn kNN usage with a user defined metric (again)
<p>Someone posted a similar question here but I couldn't get my job done</p>
<p>see</p>
<p><a href="https://stackoverflow.com/q/21052509">Sklearn kNN usage with a user defined metric</a></p>
<p>I want to define my user_metric and use it in KNN.<br />
I have a signature problem it seems but I don't understand it. thanks</p>
<pre><code>gamma=2


def mydist2 (x,y):
    z=(x-y)
    return (z[0]^2+gamma*z[1]^2) 
neigh = KNeighborsClassifier(n_neighbors=3,metric=mydist2)

neigh.fit(traindata,train_labels)
neigh.score(testdata,test_labels)
</code></pre>
<blockquote>
<p>def mydist2 (x,y):ValueError                                Traceback
(most recent call last) &lt;ipython-input-81-f934c7b5c9b3&gt; in &lt;module&gt;()<br />
→ 1 neigh.fit(traindata,train_labels)<br />
   2 neigh.score(testdata,test_labels)</p>
<p>C:\Users\Fagui\Anaconda2\lib\site-packages\sklearn\neighbors\base.pyc<br />
in fit(self, X, y)<br />
801             self._y = self._y.ravel()<br />
802<br />
<strong>803</strong>         return self._fit(X)<br />
804<br />
805</p>
<p>C:\Users\Fagui\Anaconda2\lib\site-packages\sklearn\neighbors\base.pyc<br />
in <em>fit(self, X)<br />
256             self.<em>tree = BallTree(X, self.leaf_size,<br />
257                                   metric=self.effective_metric</em>,<br />
--&gt; 258                                   **self.effective_metric_params</em>)<br />
259         elif self._fit_method == 'kd_tree':<br />
260             self._tree = KDTree(X, self.leaf_size,</p>
<p>    sklearn/neighbors/binary_tree.pxi in sklearn.neighbors.ball_tree.BinaryTree.<strong>init</strong>
(sklearn\neighbors\ball_tree.c:8381)()</p>
<p>    sklearn/neighbors/dist_metrics.pyx in sklearn.neighbors.dist_metrics.DistanceMetric.get_metric<br />
(sklearn\neighbors\dist_metrics.c:4032)()</p>
<p>    sklearn/neighbors/dist_metrics.pyx in sklearn.neighbors.dist_metrics.PyFuncDistance.<strong>init</strong><br />
(sklearn\neighbors\dist_metrics.c:10628)()</p>
<p>    ValueError: func must be a callable taking two arrays</p>
</blockquote>
<p>as a bonus question, I'd like to pass gamma as an argument</p>
<p>thanks very much</p>