Thursday, August 16, 2012

Kernel Density Estimation with scipy

This post continues the last one where we have seen how to how to fit two types of distribution functions (Normal and Rayleigh). This time we will see how to use Kernel Density Estimation (KDE) to estimate the probability density function. KDE is a non-parametric technique for density estimation in which a known density function (the kernel) is averaged across the observed data points to create a smooth approximation. Given the non-parametrica nature of KDE, the main estimator has not a fixed functional form but only it depends upon all the data points we used for the estimation. Let's see the snippet:
from scipy.stats.kde import gaussian_kde
from scipy.stats import norm
from numpy import linspace,hstack
from pylab import plot,show,hist

# creating data with two peaks
sampD1 = norm.rvs(loc=-1.0,scale=1,size=300)
sampD2 = norm.rvs(loc=2.0,scale=0.5,size=300)
samp = hstack([sampD1,sampD2])

# obtaining the pdf (my_pdf is a function!)
my_pdf = gaussian_kde(samp)

# plotting the result
x = linspace(-5,5,100)
plot(x,my_pdf(x),'r') # distribution function
hist(samp,normed=1,alpha=.3) # histogram
The result should be as follows:


  1. This comment has been removed by a blog administrator.

  2. Super helpful - thank you for posting!

  3. This comment has been removed by the author.

    1. Hi Samantha, you could have as many bins as many samples.

    2. Hello I would like to plot a histogram with counts on the y axis and binned data on the x. However once normed=1 is set my numbers change from 0 - 50 to 0 = 1.8.

  4. This comment has been removed by the author.

  5. This comment has been removed by the author.

  6. Thank you for posting. I'm currently trying to do something similar however, I'm trying to keep the count per bin data as the y axis. Do you have any hints on how to show this as the scale on the y axis?

  7. How does one plot each individual kernel?


Note: Only a member of this blog may post a comment.