This is an archival dump of old wiki content --- see scipy.org for current material

scipy.stats

The package scipy.stats provides tools for statistical analysis:

Kernel density estimation

stats.kde provides an object to do kernel density estimates (see http://www.maths.uwa.edu.au/~duongt/seminars/intro2kde/ ).

Here is an example using the gaussian kernel density estimator:

#class right

inline:kde.png

   1 from pylab import plot, figure, imshow, xlabel, ylabel, cm, show
   2 from scipy import stats, mgrid, c_, reshape, random, rot90
   3 
   4 def measure(n):
   5     """ Measurement model, return two coupled measurements.
   6     """
   7     m1 = random.normal(size=n)
   8     m2 = random.normal(scale=0.5, size=n)
   9     return m1+m2, m1-m2
  10 
  11 # Draw experiments and plot the results
  12 m1, m2 = measure(2000)
  13 xmin = m1.min()
  14 xmax = m1.max()
  15 ymin = m2.min()
  16 ymax = m2.max()
  17 
  18 # Perform a kernel density estimator on the results
  19 X, Y = mgrid[xmin:xmax:100j, ymin:ymax:100j]
  20 positions = c_[X.ravel(), Y.ravel()]
  21 values = c_[m1, m2]
  22 kernel = stats.kde.gaussian_kde(values.T)
  23 Z = reshape(kernel(positions.T).T, X.T.shape)
  24 
  25 figure(figsize=(3, 3))
  26 imshow(     rot90(Z),
  27             cmap=cm.gist_earth_r,
  28             extent=[xmin, xmax, ymin, ymax])
  29 plot(m1, m2, 'k.', markersize=2)
  30 
  31 show()