简体   繁体   English

获取numpy ndarray的平均值

[英]Get average of the numpy ndarray

I have a shape of A = (8, 64, 64, 64, 1) numpy.ndarray. 我的形状为A = (8, 64, 64, 64, 1) 8,64,64,64,1 A = (8, 64, 64, 64, 1) numpy.ndarray。 We can use np.means or np.average to calculate the means of a numpy array. 我们可以使用np.meansnp.average来计算numpy数组的均值。 But I want to get the means of the 8 (64,64,64) arrays. 但是我想获得8 (64,64,64)数组的(64,64,64) That is, i only want 8 values, calculated from the means of the (64,64,64) . 也就是说,我只想要根据(64,64,64)的平均值计算出的8个值。 Of course I can use a for loop, or use [np.means(A[i]) for i in range(A.shape[0])] . 当然,我可以使用for循环,也可以[np.means(A[i]) for i in range(A.shape[0])]使用[np.means(A[i]) for i in range(A.shape[0])] I am wondering if there is any numpy method to do this 我想知道是否有任何numpy的方法来做到这一点

You can use np.mean s axis kwarg: 您可以使用np.mean的轴kwarg:

np.mean(A, (1, 2, 3, 4))

The same works with np.average , too. 同样适用于np.average

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM