When I check the shape of an array using numpy.shape()
, I sometimes get (length,1)
and sometimes (length,)
. It looks like the differe
The point is that say a vector can be seen either as
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,)
A vector in Python is actually a two-dimensional array. It's just a coincidence that the number of rows is 1 (for row vectors), or the number of columns is 1 (for column vectors).
By contrast, a one-dimensional array is not a vector (neither a row vector nor a column vector). To understand this, think a concept in geometry, scalar. A scalar only has one attribute, which is numerical. By contrast, a vector has two attributes, number and direction. Fortunately, in linear algebra, vectors also have "directions", although only two possible directions - either horizontal or vertical (unlike infinite possible directions in geometry). A one-dimensional array only has numerical meaning - it doesn't show which direction this array is pointing to. This is why we need two-dimensional arrays to describe vectors.
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,)
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.