Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Multidimensional Kernel Density Estimation in SciPy
# -*- coding: utf-8 -*-
Created on Sun Jun 19 20:32:51 2011
import numpy as np
import scipy.stats as stats
from matplotlib.pyplot import imshow
# Create some dummy data
rvs = np.append(stats.norm.rvs(loc=2,scale=1,size=(200,1)),
kde = stats.kde.gaussian_kde(rvs.T)
# Regular grid to evaluate kde upon
x_flat = np.r_[rvs[:,0].min():rvs[:,0].max():128j]
y_flat = np.r_[rvs[:,1].min():rvs[:,1].max():128j]
x,y = np.meshgrid(x_flat,y_flat)
grid_coords = np.append(x.reshape(-1,1),y.reshape(-1,1),axis=1)
z = kde(grid_coords.T)
z = z.reshape(128,128)

markusritschel commented Aug 8, 2016

You have to import scatter from matplotlib.pyplot additionally. Anyway, nice looking example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment