python - Iterating over arbitrary dimension of numpy.array -


Is there a function to get an Iterator on an arbitrary dimension of a narrow array?

The first dimension is simple ...

 in  [63]: c = numpy.arange (24). In Renewal (2,3,4) [64]: For R: c:. ...: Print R ...: [[1 2 3] [4 5 6 7] [8 9 10 11]] [[12 13 14 15] [16 17 18 19] [20 21 22 23] ]  

But it is difficult to walk on other dimensions, for example, the last dimension:

 in  [73]: for c.swapaxes (2,0) .swapaxes (1,2): ....: print r .. ..: [[4] 8] [12 16 20]] [[1 5 9] [13 17 21]] [[6 6] [14 18 22]] [[3 7 11] [15 1923]]  

I'm making myself a generator to do this, but I wonder how to do this Nominated is not a function such as numpy.ndarray.iterdim (axis = 0) to do this automatically.

The proposal that you offer is very fast, but clearly clarity can be corrected: / P>

  for i category (c.shape [-1]): print c [:,:, i]  

or, better (faster, more Normal and more obvious):

  for category i (c.shape [-1]): print c [..., i]  

However, the first approach given above is swapax () approach:

> python -m timeit -s' The deficit; C = numpy.arange (24). C.swapaxes (2,0) for resume (2,3,4) 'C' .swapaxes (1,2): u = r '100000 loops, 3 best: 3.69 per power Python-I timed -s 'Import numerical; C = numpy.arange (24). For fibers (2,3,4) I in range (c. Size [-1]): u = c [:,:, i] '100000 loop, best 3: 6.08 per loop Python -M timetable - Import 'numeric; C = numpy.arange (24). For immature (2,3,4) '' numpy.rollaxis (c, 2): u = r '100000 loops, best 3: 6.46 per loop usec

I think It is because that is because swapaxes () does not copy any data, and because of c [:,:, i] handling > via normal code (Which handles the case where : is replaced by a more complex slice).

However, note that the second most obvious solution is c [..., i] both very legible and very fast:

  Python - I timetit-s' import numerical; C = numpy.arange (24). In the range for 'fiber (2,3,4)' '(c. Size [-1]): u = c [..., i]' 100000 loop, use best 3: 4.74 per loop  

Comments

Popular posts from this blog

MySql variables and php -

url rewriting - How to implement the returnurl like SO in PHP? -

Which Python client library should I use for CouchdB? -