计算没有for循环的numpy数组部分的平均值

时间:2018-08-16 08:14:43

标签: python numpy

我有一个NumPy数组:

a.shape: (24, 124, 117, 1)

如果我取两个内部数组的10 x 10子集

b = a[:,34:44,100:110,0]

b.shape:

(24, 10, 10)

要获取每个10 x 10数组中值的平均值的列表:

the_list = []
for i in b:
    the_list.append(np.nanmean(i))

print(the_list)
[1.3890099800025781, 1.4469500441974863, 1.3629133841057566, 1.415756533959169, 1.4254133193054614, 1.5217992758345105, 1.6735920775699795, 1.968677630129286, 2.097451850128511, 2.1386300417406416, 2.536669441756218, 2.3351648894197727, 2.794611866191022, 2.524879009293537, 3.1834270587157953, 4.049452682004729, 3.1542556793742387, 3.498059377421739, 4.439820758482223, 3.446379020262416, 4.8295208426437535, 3.7539767849251833, 5.08349934334655, 4.448043719645083]

有什么比for循环更好的方法了?

1 个答案:

答案 0 :(得分:0)

$ pip3 install jupyter

感谢Nils的发言。