开发者

numpy: calculate average in a certain area

开发者 https://www.devze.com 2023-04-10 21:01 出处:网络
is there a way for calculating the average within a certain开发者_高级运维 bbox. The difficulty is that the bbox may also contain float values, so that the bounds of the box values must be weighted. T

is there a way for calculating the average within a certain开发者_高级运维 bbox. The difficulty is that the bbox may also contain float values, so that the bounds of the box values must be weighted. The center of each cell has integer values (the edges are x.5).

Sample:

[[ 1.,  1.,  1.],
[ 1.,  1.,  1.],
[ 1.,  1.,  1.]]

bbox = minx: -0.5, miny: -0.5, maxx: 1, maxy: 1
values = 1*1 + 0.5*1 + 0.5*1 + 0.25*2
weights = 1 + 0.5 + 0.5 + 0.25
average = values / weights = 1.1111...

I couldn't figure out how to do this with numpy.average, any ideas / solutions for this problem?

Thank you very much in advance.


Your question is unclear to me but it looks like you want to be formatting an array of weights and pass it to the np.average() function along with the array of data you want to average such as:

import numpy as np

values = np.array([[1, 1, 1],
                   [2, 2, 2],
                   [3, 3, 3]])

valueweights = np.array([[1, 1, 1],
                         [0.5, 0.5, 0.5],
                         [0.25, 0.25, 0.25]])

average = np.average(values, weights=valueweights)
0

精彩评论

暂无评论...
验证码 换一张
取 消