numpy中axis的一些理解

时间:2021-07-04 21:25:23

在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])


原文:http://*.com/a/17079437

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
= b.sum(axis=2)
, then axis 2 (dimension with size 7) is collapsed, and the result has shape (5,6,8). Furthermore, c[x,y,z] is equal to the sum of all elements c[x,y,:,z].

--from