Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance/standard using 1d vs 2d vectors in numpy

Is there a standard practice for representing vectors as 1d or 2d ndarrays in NumPy? I'm moving from MATLAB which represents vectors as 2d arrays.

like image 672
bluecat Avatar asked Oct 05 '22 11:10

bluecat


2 Answers

In my experience, 1D is the norm in numpy for vectors. The only good reason to keep a vector of n elements as a 2D array of shape (1, n) or (n, 1) is in a linear algebra context, where you wanted to keep row and column vectors differentiated. As EitanT hinted on his now deleted answer, you would probably then want to use numpy's matrix type, which keeps 2D shape of returns except for single element access, e.g if a has shape (m, n) then a[0] has shape (n,) for type ndarray, but shape (1, n) for type matrix, although a[0, 0] returns a scalar in both cases.

If you stick with 1D vector of shape (n,), you can reshape on the fly for specific operations requiring the 2D shape:

a.reshape(-1, 1) # shape (n, 1)
a[:, None] # shape (n, 1)
a.reshape(1, -1) # shape (1, n)
a[None, :] # shape (1, n)

Numpy will automatically reshape your 1D vectors to shape (1, n) when broadcasting it for an operation with a 2D array involved.

like image 60
Jaime Avatar answered Oct 10 '22 01:10

Jaime


In matlab (for historical reason I would argue) the basic type is an M-by-N array (matrix) so that scalars are 1-by-1 arrays and vectors either N-by-1 or 1-by-N arrays. (Memory layout is always Fortran style).

This "limitation" is not present in numpy: you have true scalars and ndarray's can have as many dimensions you like. (Memory layout can be C or Fortran-contigous.) For this reason there is no preferred (standard) practice. It is up to you, according to your application, to choose the one which better suits your needs.

like image 37
Stefano M Avatar answered Oct 10 '22 02:10

Stefano M