[Numpy-discussion] Using nditer + external_loop to Always Iterate by Column

Eric Wieser wieser.eric+numpy at gmail.com
Tue May 19 12:11:50 EDT 2020


Hi Will,

To force an iteration to run along certain axes, I believe you should be
using `op_axes`. Your diagnosis is correct that `external_loop` is trying
to help you be more optimal, since it's purpose is exactly that:
optimization.

Unfortunately, if you use `op_axes` you'll run into
https://github.com/numpy/numpy/issues/9808.

Eric

On Tue, 19 May 2020 at 00:42, William Ayd <william.ayd at icloud.com> wrote:

> I am trying to use the nditer to traverse each column of a 2D array,
> returning the column as a 1D array. Consulting the docs, I found this
> example which works perfectly fine:
>
> In [*65*]: a = np.arange(6).reshape(2,3)
>
>
>
> In [*66*]: *for* x *in* np.nditer(a, flags=['external_loop'], order='F'):
>     ...:     print(x, end=' ')
>     ...:
>
>
> [0 3] [1 4] [2 5]
>
> When changing the shape of the input array to (1, 3) however, this doesn’t
> yield what I am hoping for any more (essentially [0], [1] [2]):
>
> In [*68*]: *for* x *in* np.nditer(a, flags=['external_loop'], order='F'):
>     ...:     print(x, end=' ')
>     ...:
>
>
> [0 1 2]
>
> I suspect this may have to do with the fact that the (1, 3) array is both
> C and F contiguous, and it is trying to return as large of a 1D
> F-contiguous array as it can. However, I didn’t see any way to really force
> it to go by columns. My best guess was the *itershape* argument though I
> couldn’t figure out how to get that to work and didn’t see much in the
> documentation.
>
> Thanks in advance for the help!
>
> - Will
>
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at python.org
> https://mail.python.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20200519/c9ad9f97/attachment-0001.html>


More information about the NumPy-Discussion mailing list