[Numpy-discussion] ndarray.T2 for 2D transpose
insertinterestingnamehere at gmail.com
Wed Apr 6 13:10:48 EDT 2016
On Tue, Apr 5, 2016 at 9:14 PM Nathaniel Smith <njs at pobox.com> wrote:
> On Tue, Apr 5, 2016 at 7:11 PM, Todd <toddrjen at gmail.com> wrote:
> > When you try to transpose a 1D array, it does nothing. This is the
> > behavior, since it transposing a 1D array is meaningless. However, this
> > often lead to unexpected errors since this is rarely what you want. You
> > convert the array to 2D, using `np.atleast_2d` or `arr[None]`, but this
> > makes simple linear algebra computations more difficult.
> > I propose adding an argument to transpose, perhaps called `expand` or
> > `expanddim`, which if `True` (it is `False` by default) will force the
> > to be at least 2D. A shortcut property, `ndarray.T2`, would be the same
> > `ndarray.transpose(True)`.
> An alternative that was mentioned in the bug tracker
> (https://github.com/numpy/numpy/issues/7495), possibly by me, would be
> to have arr.T2 act as a stacked-transpose operator, i.e. treat an arr
> with shape (..., n, m) as being a (...)-shaped stack of (n, m)
> matrices, and transpose each of those matrices, so the output shape is
> (..., m, n). And since this operation intrinsically acts on arrays
> with shape (..., n, m) then trying to apply it to a 0d or 1d array
> would be an error.
> Nathaniel J. Smith -- https://vorpus.org
I agree that we could really use a shorter syntax for a broadcasting
Swapaxes is far too verbose for something that should be so common now that
we've introduced the new matmul operator.
That said, the fact that 1-D vectors are conceptually so similar to row
makes transposing a 1-D array a potential pitfall for a lot of people. When
broadcasting along the leading dimension, a (n) shaped array and a (1, n)
array are already treated as equivalent. Treating a 1-D array like a row
transposes seems like a reasonable way to make things more intuitive for
Rather than raising an error for arrays with fewer than two dimensions, the
syntax could be made equivalent to np.swapaxes(np.atleast2d(arr), -1, -2).
the standpoint of broadcasting semantics, using atleast2d can be viewed as
allowing broadcasting along the inner dimensions. Though that's not a common
thing, at least there's a precedent.
The only downside I can see with allowing T2 to call atleast2d is that it
things like A @ b and A @ b.T2 equivalent when B is one-dimensional. That's
already the case with our current syntax though. There's some inherent
tension between the fact that broadcasting usually prepends ones to fill in
dimensions and the fact that our current linear algebra semantics often
as columns, but making 1-D arrays into rows makes a lot of sense as far as
Great ideas everyone!
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the NumPy-Discussion