[Numpy-discussion] Allow moveaxis() to take strings as axis order
Manuel Schölling
manuel.schoelling at gmx.de
Sun Jul 8 05:19:25 EDT 2018
Hi,
I have opened a pull request [1] for enhancing moveaxis() a bit, so it
can take strings as arguments to permute the axis order.
The new feature is best described by a short example. All these calls
to moveaxis() will do the same thing:
A = np.zeros((0, 1, 2, 3))
# reverse order
np.moveaxis(A, [0, 1, 2, 3], [3, 2, 1, 0])
np.moveaxis(A, 'ijkl', 'lkji')
np.moveaxis(A, 'wxyz', 'zyxw')
# more complicated axis permutation
np.moveaxis(A, [0, 1, 2, 3], [3, 1, 2, 0])
np.moveaxis(A, 'ijkl', 'ljki')
np.moveaxis(A, 'wxyz', 'zxyw')
It has been mentioned by Eric Wieser that
np.einsum('ijkl->lkji', A)
basically does the same thing, although it is not really obvious to the
user that is not used Einstein's sum convention what this call does,
because the function call says it would perform a 'sum' operation, but
actually only permutes axes.
It was also mentioned that the Zen of Python says
> There should be one-- and preferably only one --obvious way to do it.
and if moveaxis() supports multiple ways to perform its operation,
there could be a cognitive bias if the developer is only used to e.g.
the string arguments and suddenly see the array arguments (or vice
versa).
Despite these arguments I think the string arguments makes calls to
moveaxis() much more obvious than integer arguments, especially if ndim
> 3.
How does the mailing list feel about this pull request?
Bye,
Manuel
[1] https://github.com/numpy/numpy/pull/11504
More information about the NumPy-Discussion
mailing list