I'm trying to perform a Kernel Density Estimation on my histogram which has been computed over an image:
I use scikit learn to compute the kernel density estimation using a gaussian kernel:
histogram = np.histogram(img, bins=256, range=(0,255), normed=False)
X = histogram[0][:, np.newaxis]
X_plot = np.linspace(0,255,256,)[:, np.newaxis]
kde = KernelDensity(kernel='gaussian', bandwidth=0.5).fit(X)
log_dens = kde.score_samples(X_plot)
res = np.exp(log_dens)
However, when I plot 'res', I only get its first 3/4 values which differ from 0. I do not understand why I do not get a good estimate while I have followed the instructions given here:
http://scikit-learn.org/stable/auto_examples/neighbors/plot_kde_1d.html
As you have mentioned, you don't need to do the histogram step. Given a set of samples, KernelDensity.fit
estimate the density. Then, you simply plot the density estimation on a predefined grid.
plt.figure()
X_plot = np.arange(255)[:, None] # predefined grid
# estimate density on samples
kde = KernelDensity(kernel='gaussian', bandwidth=0.2).fit(X.ravel()[:, None])
log_dens = kde.score_samples(X_plot) # evaluate the density model on the data.
plt.plot(np.exp(log_dens))
plt.show()