[scikit-learn] Does KernelDensity works for a vector with two elements?

Mahmood Naderan mahmood.nt at gmail.com
Tue Mar 1 06:34:28 EST 2022


Hi,
I would like to use KernelDensity for some vectors. If the length of
vectors is greater than 2, there is no problem.
However, for the following example, it seems that the density
estimation doesn't work properly.

v = [2.46415e+07,1.23208e+07]
a = array(v).reshape(-1, 1)
kde = KernelDensity(kernel='gaussian', bandwidth=1).fit(a)
s = linspace(min(a),max(a))
e = kde.score_samples(s.reshape(-1,1))
plot(s, e)
mi = argrelextrema(e, np.less)[0]
print ("Minima:", s[mi])


The s[mi] is empty in the end. But indeed the plot shows a minima
because there is a gap between two numbers.
Is there any restriction or note about using KernelDensity?

Regards,
Mahmood


More information about the scikit-learn mailing list