Sample_List=np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16])
Required to take the average of every 4
like 1,2,3,4 average is 2.5
followed by 5,6,7,8 is 6.5
followed by 9,10,11,12 is 10.5
followed by 13,14,15,16 is 14,5
Exexted输出:
[2.5, 6.5, 10.5, 14.5]
到目前为止,我试着参考这个问题Average of each consecutive segment in a list
Calculate the sum of every 5 elements in a python array
2条答案
按热度按时间cngwdvgl1#
使用
reshape
。在下面的示例中,RESHAPE(-1,4)表示每行4个元素输出
zhte4eai2#
有多种方法可以做到这一点。考虑到
sample_list
如下所示将在下面留下两个选项,这两个选项将允许计算数值数组中每4个元素的平均值。
选项1
选项2
使用
numpy.mean