I am wondering why in numpy there are one dimensional array of dimension (length, 1) and also one dimensional array of dimension (length, ) w/o a second value.
I am running into this quite frequently, e.g. when using
The data of a
ndarray is stored as a 1d buffer - just a block of memory. The multidimensional nature of the array is produced by the
strides attributes, and the code that uses them.
numpy developers chose to allow for an arbitrary number of dimensions, so the shape and strides are represented as tuples of any length, including 0 and 1.
In contrast MATLAB was built around FORTRAN programs that were developed for matrix operations. In the early days everything in MATLAB was a 2d matrix. Around 2000 (v3.5) it was generalized to allow more than 2d, but never less. The
np.matrix still follows that old 2d MATLAB constraint.
If you come from a MATLAB world you are used to these 2 dimensions, and the distinction between a row vector and column vector. But in math and physics that isn't influenced by MATLAB, a vector is a 1d array. Python lists are inherently 1d, as are
c arrays. To get 2d you have to have lists of lists or arrays of pointers to arrays, with
x style of indexing.
Look at the shape and strides of this array and its variants:
In : x=np.arange(10) In : x.shape Out: (10,) In : x.strides Out: (4,) In : x1=x.reshape(10,1) In : x1.shape Out: (10, 1) In : x1.strides Out: (4, 4) In : x2=np.concatenate((x1,x1),axis=1) In : x2.shape Out: (10, 2) In : x2.strides Out: (8, 4)
MATLAB adds new dimensions at the end. It orders its values like a
order='F' array, and can readily change a (n,1) matrix to a (n,1,1,1).
numpy is default
order='C', and readily expands an array dimension at the start. Understanding this is essential when taking advantage of broadcasting.
x1 + x is a (10,1)+(10,) => (10,1)+(1,10) => (10,10)
Because of broadcasting a
(n,) array is more like a
(1,n) one than a
(n,1) one. A 1d array is more like a row matrix than a column one.
In : np.matrix(x) Out: matrix([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]) In : _.shape Out: (1, 10)
The point with
concatenate is that it requires matching dimensions. It does not use broadcasting to adjust dimensions. There are a bunch of
stack functions that ease this constraint, but they do so by adjusting the dimensions before using
concatenate. Look at their code (readable Python).
So a proficient numpy user needs to be comfortable with that generalized
shape tuple, including the empty
() (0d array),
(n,) 1d, and up. For more advanced stuff understanding strides helps as well (look for example at the strides and shape of a transpose).