Reputation: 452
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
Upvotes: 2
Views: 1915
Reputation: 114518
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.
Upvotes: 3
Reputation: 15738
# 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
Upvotes: 1