[scikit-learn] Using KernelDensity for spatial clustering

Mahmood Naderan mahmood.nt at gmail.com
Tue Jan 25 10:58:38 EST 2022


Hi
I would like to group a set of numbers by their spatial distance and
since it is a 1D problem, I want to use KernelDensity. Based on the
examples I saw in the internet [1], the following code, works:


a = array([1,2,3,10,12,11,50,51,52,53]).reshape(-1, 1)
kde = KernelDensity(kernel='gaussian').fit(a)
s = linspace(0,100)
e = kde.score_samples(s.reshape(-1,1))
mi = argrelextrema(e, np.less)[0]


With the code, I am able to find the minima of the KDE and then
compare the values in `a` with `s[mi]`. For simple and test file, that
works.
However, I need some more information about the way it works. For
example, if I change the linespace boundries, the result changes. What
does the linespace do exactly?

[1] https://scikit-learn.org/stable/auto_examples/neighbors/plot_kde_1d.html

Regards,
Mahmood


More information about the scikit-learn mailing list