what does numpy ndarray shape do?
Asked Answered
S

4

59

I have a simple question about the .shape function, which confused me a lot.

a = np.array([1, 2, 3])   # Create a rank 1 array
print(type(a))            # Prints "<class 'numpy.ndarray'>"
print(a.shape)            # Prints "(3,)"

b = np.array([[1,2,3],[4,5,6]])    # Create a rank 2 array
print(b.shape)                     # Prints "(2, 3)"

What did the .shape exactly do? count how many rows, how many columns, then the a.shape suppose to be, (1,3), one row three columns, right?

Scurvy answered 30/11, 2017 at 1:28 Comment(0)
P
82

yourarray.shape or np.shape() or np.ma.shape() returns the shape of your ndarray as a tuple; And you can get the (number of) dimensions of your array using yourarray.ndim or np.ndim(). (i.e. it gives the n of the ndarray since all arrays in NumPy are just n-dimensional arrays (shortly called as ndarrays))

For a 1D array, the shape would be (n,) where n is the number of elements in your array.

For a 2D array, the shape would be (n,m) where n is the number of rows and m is the number of columns in your array.

Please note that in 1D case, the shape would simply be (n, ) instead of what you said as either (1, n) or (n, 1) for row and column vectors respectively.

This is to follow the convention that:

For 1D array, return a shape tuple with only 1 element   (i.e. (n,))
For 2D array, return a shape tuple with only 2 elements (i.e. (n,m))
For 3D array, return a shape tuple with only 3 elements (i.e. (n,m,k))
For 4D array, return a shape tuple with only 4 elements (i.e. (n,m,k,j))

and so on.

Also, please see the example below to see how np.shape() or np.ma.shape() behaves with 1D arrays and scalars:

# sample array
In [10]: u = np.arange(10)

# get its shape
In [11]: np.shape(u)    # u.shape
Out[11]: (10,)

# get array dimension using `np.ndim`
In [12]: np.ndim(u)
Out[12]: 1

In [13]: np.shape(np.mean(u))
Out[13]: ()       # empty tuple (to indicate that a scalar is a 0D array).

# check using `numpy.ndim`
In [14]: np.ndim(np.mean(u))
Out[14]: 0

P.S.: So, the shape tuple is consistent with our understanding of dimensions of space, at least mathematically.

Performative answered 30/11, 2017 at 1:44 Comment(6)
I think your explanation would be even more complete by adding in the documentation for .shape and probably including .ndim as well: docs.scipy.org/doc/numpy-1.13.0/reference/generated/…Guttery
"you can get the dimensions of your array using yourarray.ndim or np.ndim()." --- I have tried and it only gives one of dimensions, not the dimensions.Illegitimacy
@DmitriZaitsev, I think they mistyped. ndim returns the number of dimensions, not the dimensions or even any of the dimensions. So for a 1D array, it would return 1; for 2D, 2, 3D, 3; and so on. The documentation already linked backs this up.Synovia
Uuslaly M refers to rows, no? Otherwise this is super helpful.Condolent
Why is there a comma in the tuple for the shape of a rank-1 ndarray?Macron
@Macron I'd prefer the term 1D array. Rank has a different meaning in matrix algebra. Regarding your question about comma: np.shape(arr) or arr.shape always return a tuple object. For 0D arrays, it's empty parens (), for 1D arrays it's a tuple with one element (elem1, ), for 2D arrays it's a tuple with two elements (elem1, elem2), and so on. In Python, we cannot represent a single item tuple without a trailing comma. That's the reason. For more info, read about tuples in Python :)Performative
I
5

Unlike it's most popular commercial competitor, numpy pretty much from the outset is about "arbitrary-dimensional" arrays, that's why the core class is called ndarray. You can check the dimensionality of a numpy array using the .ndim property. The .shape property is a tuple of length .ndim containing the length of each dimensions. Currently, numpy can handle up to 32 dimensions:

a = np.ones(32*(1,))
a
# array([[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[ 1.]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]])
a.shape
# (1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
a.ndim
# 32

If a numpy array happens to be 2d like your second example, then it's appropriate to think about it in terms of rows and columns. But a 1d array in numpy is truly 1d, no rows or columns.

If you want something like a row or column vector you can achieve this by creating a 2d array with one of its dimensions equal to 1.

a = np.array([[1,2,3]]) # a 'row vector'
b = np.array([[1],[2],[3]]) # a 'column vector'
# or if you don't want to type so many brackets:
b = np.array([[1,2,3]]).T
Institutive answered 30/11, 2017 at 1:49 Comment(0)
S
5
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])

enter image description here

Streaky answered 20/4, 2020 at 14:54 Comment(0)
P
1

.shape() gives the actual shape of your array in terms of no of elements in it, No of rows/No of Columns. The answer you get is in the form of tuples.

For Example: 1D ARRAY:

d=np.array([1,2,3,4])
print(d)
(1,)

Output: (4,) ie the number4 denotes the no of elements in the 1D Array.

2D Array:

e=np.array([[1,2,3],[4,5,6]])   
print(e)
(2,3)

Output: (2,3) ie the number of rows and the number of columns.

The number of elements in the final output will depend on the number of rows in the Array....it goes on increasing gradually.

Primaveras answered 2/10, 2019 at 3:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.