numpy 1 .
:
func = np.sum
for fiber_index in np.ndindex(A.shape[:-1]):
print func(fiber_index)
print A[fiber_index]
- :
(0, 0)
[0 1 2]
(0, 1)
[3 4 5]
(0, 2)
[6 7 8]
...
1- 2- , 1D- .
ndindex. . fooobar.com/questions/533771/....
as_strided , nditer. "multi_index" , . __next__. , numpy .
http://docs.scipy.org/doc/numpy-dev/reference/arrays.nditer.html
Iterating Over Arrays , cython.
, sum, max, product, , () . , sum, :
np.sum(A, axis=-1)
np.sum(A, axis=(1,2))
np.add.reduce(A, axis=-1)
np.add ufunc, reduce . ufunc - accumulate, reduceat. ufunc.
xnx
np.apply_along_axis(np.sum, 2, A)
apply_along_axis, , A. i, j while, :
outarr[(i,j)] = np.sum(A[(i, j, slice(None))])
slice - . , , . , .
. "" , .
def with_ndindex(A, func, ax=-1):
A = np.rollaxis(A, ax, A.ndim)
shape = A.shape[:-1]
B = np.empty(shape,dtype=A.dtype)
for ii in np.ndindex(shape):
B[ii] = func(A[ii])
return B
3x3x3, 10x10x10 100x100x100 A. np.ndindex , apply_along_axis. np.sum(A, -1) .
, func 1D- ( sum), ndindex .