块状:在“锯齿状"区域中一维的平均值. 3D阵列 [英] Numpy: average over one dimension in "jagged" 3D array

查看:108
本文介绍了块状:在“锯齿状"区域中一维的平均值. 3D阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个N * M * X维数组"data",其中N和M是固定的,但X对于每个条目数据[n] [m]都是可变的.

Suppose I have an N*M*X-dimensional array "data", where N and M are fixed, but X is variable for each entry data[n][m].

(为澄清起见,我只是在用于读取数据的3D python列表上使用了np.array(),因此numpy数组的尺寸为N * M,其条目为变长列表)

( To clarify, I just used np.array() on the 3D python list which I used for reading in the data, so the numpy array is of dimensions N*M and its entries are variable-length lists)

我现在想计算X维度上的平均值,以便剩下一个N * M维数组.将np.average/mean与轴参数一起使用不起作用,所以我现在的方式是对N和M进行迭代,然后将手动计算的平均值附加到新列表中,但这是不可行的感觉很蟒蛇":

I'd now like to compute the average over the X-dimension, so that I'm left with an N*M-dimensional array. Using np.average/mean with the axis-argument doesn't work, so the way I'm doing it right now is just iterating over N and M and appending the manually computed average to a new list, but that just doesn't feel very "python":

avgData=[]
for n in data:
    temp=[]
    for m in n:
        temp.append(np.average(m))
    avgData.append(temp)

我在这里缺少明显的东西吗?我正在尝试提高自己的python技能,因此有趣/多样化的响应非常受欢迎! :)

Am I missing something obvious here? I'm trying to freshen up my python skills while I'm at it, so interesting/varied responses are more than welcome! :)

谢谢!

推荐答案

使用 np.vectorize :

do_avg = np.vectorize(np.average)
data_2d = do_avg(data)

这篇关于块状:在“锯齿状"区域中一维的平均值. 3D阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆