frankgut
frankgut

Reputation: 125

Get median value in each bin in a 2D grid

I have a 2-D array of coordinates and each coordinates correspond to a value z (like z=f(x,y)). Now I want to divide this whole 2-D coordinate set into, for example, 100 even bins. And calculate the median value of z in each bin. Then use scipy.interpolate.griddata function to create a interpolated z surface. How can I achieve it in python? I was thinking of using np.histogram2d but I think there is no median function in it. And I found myself have hard time understanding how scipy.stats.binned_statistic work. Can someone help me please. Thanks.

Upvotes: 1

Views: 1603

Answers (2)

Antonio Ragagnin
Antonio Ragagnin

Reputation: 2327

With numpy.histogram2d you can both count the number of data and sum it, thus it gives you the possibility to compute the average.

I would try something like this:

import numpy as np
coo=np.array([np.arange(1000),np.arange(1000)]).T #your array coordinates

def func(x, y): return x*(1-x)*np.sin(np.pi*x) / (1.5+np.sin(2*np.pi*y**2)**2)

z = func(coo[:,0], coo[:,1])

(n,ex,ey)=np.histogram2d(coo[:,0], coo[:,1],bins=100) # here we get counting
(tot,ex,ey)=np.histogram2d(coo[:,0], coo[:,1],bins=100,weights=z) # here we get total over z
average=tot/n
average=np.nan_to_num(average) #cure 0/0 
print(average)

Upvotes: 2

Ryan Cook
Ryan Cook

Reputation: 179

you'll need a few functions or one depending on how you want to structure things:

  1. function to create the bins should take in your data, determine how big each bin is and return an array or array of arrays (also called lists in python).

    Happy to help with this but would need more information about the data.

  2. get the median of the bins: Numpy (part of scipy) has a median function http://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.median.html essentially the median on an array called "bin" would be: $ numpy.median(bin)

Note: numpy.median does accept multiple arrays, so you could get the median for some or all of your bins at once. numpy.median(bins) which would return an array of the median for each bin

Updated

Not 100% on your example code, so here goes:

import numpy as np
# added some parenthesis as I wasn't sure of the math. also removed ;'s
def bincalc(x, y): 
    return x*(1-x)*(np.sin(np.pi*x))/(1.5+np.sin(2*(np.pi*y)**2)**2)

coo = np.random.rand(1000,2)
tcoo = coo[0]
a = []
for i in tcoo:
    a.append(bincalc(coo[0],coo[1]))
z_med = np.median(a)
print(z_med)`

Upvotes: 1

Related Questions