---
title: "Numpy Tranpose Explained"
canonical: "/2017/09/01/numpy-shapes-explained/"
date: "2017-09-01"
author: "Alok Singh"
tags: []
---


## `(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](https://en.wikipedia.org/wiki/Transpose_of_a_linear_map).

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 vector[^1]. 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 explicitly[^2].



[^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.
