ENH: ndarray.T2 for 2D transpose.
See original GitHub issueWhen 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)
.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:5 (5 by maintainers)
Top Results From Across the Web
numpy.transpose — NumPy v1.24 Manual
Returns an array with axes transposed. For a 1-D array, this returns an unchanged view of the original array, as a transposed vector...
Read more >Transpose ndarray (swap rows and columns, rearrange axes)
To transpose NumPy array ndarray (swap rows and columns), use the T attribute (.T), the ndarray method transpose() and the numpy.transpose() ...
Read more >Supported NumPy features - Numba
Basic linear algebra is supported on 1-D and 2-D contiguous arrays of floating-point and complex numbers: numpy.dot() · numpy.kron() ('C' and 'F' order...
Read more >jax.numpy.transpose - JAX documentation
For an array a with two axes, transpose(a) gives the matrix transpose. Refer to numpy.ndarray.transpose for full documentation. Parameters. a (array_like) – ...
Read more >Border Handling for 2D Transpose Filter Structures on an FPGA
Section 2 summarizes previously reported work on FPGA architectures for handling image borders. Two novel transpose-form border handling ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’ve known a few other people who think that the behavior of transpose for 1-D arrays isn’t as useful as it could be. The current behavior is somewhat counter intuitive, especially for beginners. +1 to providing an API that helps alleviate that.
I agree with the improvement suggestion as well. It’d be really nice if we could also make it do a broadcasting 2D transpose by swapping the last two dimensions of any higher dimensional array rather than reversing the dimensions the way transpose currently does. When chaining together a series of gufunc based operations to apply a linear algebra-like transformation, it seems odd that the idiom for a transpose is
np.swapaxes(a, 1, 2)
. For example, the expressiona @ b.T
with 2D arrays could follow gufunc-like semantics when expressed asa @ b.T2
rather thana @ np.swapaxes(b, -1, -2)
.All that said, a mailing list post is better for discussing something like this.
There’s some discussion of details here: https://github.com/numpy/numpy/issues/9530