one-dimensional array shapes (length,) vs. (length,1) vs. (length)
The point is that say a vector can be seen either as
- a vector
- a matrix with only one column
- a 3 dimensional array where the 2nd and 3rd dimensions have length one
- ...
You can add dimensions using [:, np.newaxis]
syntax or drop dimensions using np.squeeze
:
>>> xs = np.array([1, 2, 3, 4, 5])
>>> xs.shape
(5,)
>>> xs[:, np.newaxis].shape # a matrix with only one column
(5, 1)
>>> xs[np.newaxis, :].shape # a matrix with only one row
(1, 5)
>>> xs[:, np.newaxis, np.newaxis].shape # a 3 dimensional array
(5, 1, 1)
>>> np.squeeze(xs[:, np.newaxis, np.newaxis]).shape
(5,)
In Python, (length,)
is a tuple, with one 1 item. (length)
is just parenthesis around a number.
In numpy
, an array can have any number of dimensions, 0, 1, 2, etc. You are asking about the difference between 1 and 2 dimensional objects. (length,1)
is a 2 item tuple, giving you the dimensions of a 2d array.
If you are used to working with MATLAB, you might be confused by the fact that there, all arrays are 2 dimensional or larger.
The (length,) array is an array where each element is a number and there are length elements in the array. The (length, 1) array is an array which also has length elements, but each element itself is an array with a single element. For example, the following uses length=3.
>>> import numpy as np
>>> a = np.array( [[1],[2],[3]] )
>>> a.shape
>>> (3, 1)
>>> b = np.array( [1,2,3] )
>>> b.shape
>>> (3,)