As far as I know, PDFs always have positive co-domains, but here is an example of one that outputs negative numbers:


from sklearn.neighbors.kde import KernelDensity
import numpy as np
X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
kde = KernelDensity(kernel='gaussian', bandwidth=0.2).fit(X)
array([-0.41075698, -0.41075698, -0.41076071, -0.41075698, -0.41075698,

Any idea what's going on? And what's the solution?

Here is me trying to do the same but with my own data: enter image description here


The results are negative because score_samples() returns the log density.

From the help message:

density : ndarray, shape (n_samples,)
    The array of log(density) evaluations
| cite | improve this answer | |
  • $\begingroup$ Is this common practice? Currently I am doing this: f = [pow(i, math.e) for i in kde.score_samples(X)] -- is there any better solution? And thanks : $\endgroup$ – caveman Jun 11 '16 at 19:41
  • 1
    $\begingroup$ No, pow(i, math.e) raises $i$ to the power of $e$. You want the reverse. But, it's better to use the exponential function than to manually raise $e$ to the power of $i$. Instead of using a list comprehension, just use NumPy. This will let you operate on entire vectors (and give much better performance). f = numpy.exp(kde.score_examples(X)) returns the entire vector of results. $\endgroup$ – user20160 Jun 11 '16 at 20:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.