Numpy Tranpose Explained


(n,) vs. (n, 1)

Numpy arrays are set up so 0D arrays are scalars, 1D arrays are vectors, 2D arrays are matrices (and the linear map associated with them), and higher dimensional arrays are unimportant for this post.

This can be seen from the length of np.array(...).shape. np.array(1).shape is (), a zero length tuple, np.array([1]).shape is a length one tuple, and you get the idea from there.

Transposing

The transpose of a 1D array of shape (n,) is still a 1D array of the same shape, rather than a 2D array of shape (1,n). In other words, transposing 1D arrays doesn’t do anything.

This will only really make sense if you understand the transpose and its relationship to dual spaces.

Numpy uses the transpose only to express the transpose of a linear map, not as a handy way to get a linear functional (vector in the dual space) from your vector1. To do that, you have to explicitly make your vector into a linear map (of shape (n,1)) with reshape and then transpose.

In this way, Numpy draws a sharp line between vectors and the linear maps acting on them. All isomorphisms that allow you to conflate the two don’t fly in Numpy, so you’ll have to do the conversions explicitly2.

  1. Under the dual basis induced by whichever one you’re using to represent your original vector. 

  2. Which I’m personally a fan of when programming. 

Related Posts

Use of emphasis in speech

Generating a lot of language data with a theorem prover

"Litany Against Fear" in Present Tense

When it's time to party we will party hard

these are people who died

divine carrot

the frog

what it’s like to get nail phenolization

Why 0 to the power of 0 is 1

Lines and Points are Circles