Python numpy array 'size' confuses me a lot
a = np.array([1,2,3])
a.size = (3, )
------------------------
b = np.array([[2,1,3,5],
[2,2,5,1],
[3,6,99,5]])
b.size = (3,4)
'b' makes sense since it has 3 rows and 4 columns in each
But how is 'a' size = (3, ) ? Shouldn't it be (1,3) since its 1 row and 3 columns?
No, a numpy.ndarray
with shape (1, 3) would look like:
np.array([[1,2,3]])
Think about how the shape corresponds to indexing:
arr[0, ...] #First row
I still have three more options, namely:
arr[0,0]
arr[0,1]
arr[0,2]
Try doing that with a 1 dimensional array
You should resist the urge to think of numpy
arrays as having rows and columns, but instead consider them as having dimensions and shape. This is an important point which differentiates np.array
and np.matrix
:
x = np.array([1, 2, 3])
print(x.ndim, x.shape) # 1 (3,)
y = np.matrix([1, 2, 3])
print(y.ndim, y.shape) # 2 (1, 3)
An n-D array can only use n integer(s) to represent its shape. Therefore, a 1-D array only uses 1 integer to specify its shape.
In practice, combining calculations between 1-D and 2-D arrays is not a problem for numpy
, and syntactically clean since @
matrix operation was introduced in Python 3.5. Therefore, there is rarely a need to resort to np.matrix
in order to satisfy the urge to see expected row and column counts.
In the rare instances where 2 dimensions are required, you can still use numpy.array
with some manipulation:
a = np.array([1, 2, 3])[:, None] # equivalent to np.array([[1], [2], [3]])
print(a.ndim, a.shape) # 2 (3, 1)
b = np.array([[1, 2, 3]]) # equivalent to np.array([1, 2, 3])[:, None].T
print(b.ndim, b.shape) # 2 (1, 3)
I think you meant ndarray.shape
. In that case, there's no need for confusion. Quoting the documentation from ndarray.shape
:
Tuple of array dimensions.
ndarray.shape
simply returns a shape tuple.
In [21]: a.shape
Out[21]: (3,)
This simply means that a
is an 1D array with 3 entries.
If the shape tuple returns it as (1,3)
then a
would become a 2D array. For that you need to use:
In [23]: a = a[np.newaxis, :]
In [24]: a.shape
Out[24]: (1, 3)
Since array b
is 2D, the shape tuple has two entries.
In [22]: b.shape
Out[22]: (3, 4)