question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ENH: ndarray.T2 for 2D transpose.

See original GitHub issue

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).

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:1
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
insertinterestingnameherecommented, Apr 1, 2016

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 expression a @ b.T with 2D arrays could follow gufunc-like semantics when expressed as a @ b.T2 rather than a @ np.swapaxes(b, -1, -2).

All that said, a mailing list post is better for discussing something like this.

0reactions
njsmithcommented, Aug 8, 2017

There’s some discussion of details here: https://github.com/numpy/numpy/issues/9530

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found