'Soft binning with numpy histogramdd

I am trying to soft bin my data using np.histogramdd, but I could not find a solution anywhere.

Here is what I mean. Say I have my data:

data = np.array([[1.8, 3.1]])

If I compute the histogram as follows

h, _ = np.histogramdd(data, bins=(5,5), range=[(0,5), (0,5)])

the output is

array([[0., 0., 0., 0., 0.],
       [0., 0., 0., 1., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.]])

Since NumPy uses hard binning, a bin gets either a 1 or a 0 (in this case) depending on if a data point lands in that bucket. Take, for instance, the row with a 1 since the first data point [1.8, 3.1] falls within bin=(1, 3) it gets a 1 in the first-row third col (index starts at 0).

However, I would like to use soft binning. That is, I want the output to be

array([[0., 0., 0.,   0.,   0.],
       [0., 0., 0.28, 0.42, 0.],
       [0., 0., 0.12, 0.18, 0.],
       [0., 0., 0.,   0.,   0.],
       [0., 0., 0.,   0.,   0.]])

That is since 1.8 is closer to row-1 bin center than to row-2 bin center, (1.8 - 1.5 = .3 < 2.5 - 1.8 = .7) row-1 gets .7 while row-2 gets .3. Now we have to compute the cols. Since 3.1 is closer to 3.5 than to 2.5 (row-1,col-3) gets .6*.7=.42 (row-1,col-2) gets .4*.7=.28. We can compute the same proprtion for row-2 to get my desiered results.

How do I accomplish this? Any ideas?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source