2

Let's say I have a heatmap of probability density function as a numpy ndarray(m,n). Is there a function that automatically computes mean treating this matrix as probability density? I can't seem to find a function that would automatically do that.

Of course an easy solution would be to create a weighted average with weights equal to indices of the array, but seems like there should be a built-in function that would just do that.

Clasification: Say my array is heatmap = [[0,0,0,1],[0,0,1,0],[0,0,1.5,0],[0,0,0,0]]. No if we assume that this is not normalized probability you can calculate mean and other properties of the probability density.

For example mean in x direction would be

    xx = np.arange(0,heatmap.shape[1],1)
    weights = np.tile(xx,(heatmap.shape[0],1))

    mean_x = np.average(weights, weights = heatmap_avg_left)

I'm just looking for a function in numpy or scipy that would do this and other probability properties automatically

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
Mike Azatov
  • 402
  • 6
  • 22

2 Answers2

3

You can find the center-of-mass of an array using scipy.ndimage.center_of_mass. If your array is indexed into a map containing the individual masses, you can transform directly:

from scipy.ndimage import center_of_mass

indices = ... # shape (m, n), values in [0, k)
mass_lookup = ... # shape (k)

result = center_of_mass(mass_lookup[indices])

In this case, mass_lookup is like a heat map. If the array contains weights instead, use it directly.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
  • Could you please explain the difference between a heat map and an array of weights? – Stef Sep 20 '21 at 15:33
  • 1
    @Stef. I'm describing a situation where the integer label in the array corresponds to an index into `mass_lookup`, which is a simple lookup table. – Mad Physicist Sep 20 '21 at 17:33
1
# assuming the array is called pdf_arr
axis = 0  # 0 for x axis (columns), 1 for y axis (rows)
marginal_pdf = pdf_arr.sum(axis=axis)
# since it's just a sample, normalize pdf
marginal_pdf /= marginal_pdf.sum()
axis_mean = (marginal_pdf * np.arange(pdf_arr.shape[1-axis])).sum()
# repeat the same for axis = 1
Marat
  • 15,215
  • 2
  • 39
  • 48
  • Thanks, this would work. I was just hoping there would be a built-in function for working with probability densities. – Mike Azatov Jan 04 '20 at 01:46