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 am moving from MATLAB, which represents vectors as 2d arrays.

+4
source share
2 answers

In my experience, 1D is the norm in numpy for vectors. The only good reason to save the element vector n as a 2D array of form (1, n) or (n, 1) in the context of linear algebra, where you would like to differentiate row and column vectors. Since EitanT hinted at his now deleted answer, you probably want to use the numpy matrix type, which saves a 2D return form, except for access to one element, for example, if a has the form (m, n) , then a[0] has the form (n,) for the type ndarray , but the form (1, n) for the type matrix , although a[0, 0] returns the scalar in both cases.

If you stick to the 1D shape vector (n,) , you can redo it on the fly to perform certain operations that require a 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 convert your 1D vectors to form (1, n) when broadcasting for operations with a 2D array.

+3
source

In Matlab (for historical reasons, I would argue) the main type is an array (matrix) M -by- N , so that scalars are 1-by-1 arrays and vectors are either N -by-1 or 1-by- N . (The memory format always has a Fortran style).

This "limitation" is missing in numpy : you have true scalars, and ndarray can be as many sizes as you like. (The memory layout may be C or Fortran-contigous.) For this reason, there is no preferred (standard) practice. According to your expression, you can choose the one that best suits your needs.

+1
source

All Articles