I have an n-dimensional array in NumPy. I would like to calculate the sum of all the elements in a slice. For instance:
if I have the following 2x2x2 3D array having the values:
z = 1
1 2
3 4
z = 2
5 6
7 8
When I get a slice for z = 1 I would like to get 10 and for z = 2 I would like to get 26.
I can use the following for a 3D space but how can I do that for n dimensional space?
(array.sum(axis = 0)).sum(axis = 0)
To sum the
ith slice along thedth dimension:arr.take([i], axis=d).sum()