在numpy中,.sum(axis=n)解释:
如果,b是一个shap(5, 6, 7, 8)的numpy array,
然后,c = b.sum(axis=2)
那么,c的shape将是(5, 6, 8) ,因为“7”就是axis=2,被清除了。
而且,c[x, y, z] = sum( b[x, y, : , z])
If you do .sum(axis=n)
, for example, then dimension n
is collapsed and deleted, with all values in the new matrix equal to the sum of the corresponding collapsed values. For example, if b
has shape (5,6,7,8)
, and you do c
, then axis 2 (dimension with size 7) is collapsed, and the result has shape
= b.sum(axis=2)(5,6,8)
. Furthermore, c[x,y,z]
is equal to the sum of all elements c[x,y,:,z]
.
--from