[Numpy-discussion] ndarray.T2 for 2D transpose

Nathaniel Smith njs at pobox.com
Tue Apr 5 23:14:23 EDT 2016

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 correct
> behavior, since it transposing a 1D array is meaningless.  However, this can
> often lead to unexpected errors since this is rarely what you want.  You can
> 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 array
> to be at least 2D.  A shortcut property, `ndarray.T2`, would be the same as
> `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

More information about the NumPy-Discussion mailing list