# Why is KDE output negative?

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

http://scikit-learn.org/stable/modules/density.html

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)
kde.score_samples(X)
array([-0.41075698, -0.41075698, -0.41076071, -0.41075698, -0.41075698,
-0.41076071])


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

Here is me trying to do the same but with my own data:

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

• 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. – user20160 Jun 11 '16 at 20:13