You could just need “regular" Python function that outputs a scalar. For
example, consider the following example:
>>> from sklearn.neighbors import NearestNeighbors
>>> import numpy as np
>>> X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
>>> nbrs = NearestNeighbors(n_neighbors=2, algorithm='ball_tree').fit(X)
>>> distances, indices = nbrs.kneighbors(X)
>>> distances
array([[ 0. , 1. ],
[ 0. , 1. ],
[ 0. , 1.41421356],
[ 0. , 1. ],
[ 0. , 1. ],
[ 0. , 1.41421356]])
(note that I am using the NearestNeighbors class here, but the same applies to
the KNeighborsClassifier)
For example, to compute the distances between samples as Euclidean distance
(the default) you could just define a Python function
>>> def eucldist(x, y):
... return np.sqrt(np.sum((x-y)**2))
>>> nbrs = NearestNeighbors(n_neighbors=2, algorithm='ball_tree',
>>> metric=eucldist).fit(X)
>>> distances, indices = nbrs.kneighbors(X)
>>> distances
array([[ 0. , 1. ],
[ 0. , 1. ],
[ 0. , 1.41421356],
[ 0. , 1. ],
[ 0. , 1. ],
[ 0. , 1.41421356]])
(alt. you could provide it as lambda function)
Best,
Sebastian
> On Jan 8, 2016, at 9:19 PM, A neuman <[email protected]> wrote:
>
> Hello everyone,
>
> I actually want to use the KNeighboursClassifier, with my own distances.
>
> in the Documentation stands the following:
>
> [callable] : a user-defined function which accepts an array of distances, and
> returns an array of the same shape containing the weights.
>
> I just dont know, how should the array looks like?
>
> For example, if I have 100 Samples, the array has a size 100*100?
> So for every samples there is a distance to the other 99 samples.
>
> [[0.4, 0.2, ...],[0.3,0.1,...]........[0.9,0.6,...]] something like this?
>
> I would appreciate your help.
>
> best,
>
>
> ------------------------------------------------------------------------------
> Site24x7 APM Insight: Get Deep Visibility into Application Performance
> APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
> Monitor end-to-end web transactions and take corrective actions now
> Troubleshoot faster and improve end-user experience. Signup Now!
> http://pubads.g.doubleclick.net/gampad/clk?id=267308311&iu=/4140_______________________________________________
> Scikit-learn-general mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/scikit-learn-general
------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=267308311&iu=/4140
_______________________________________________
Scikit-learn-general mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/scikit-learn-general