Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does numpy ndarray shape do?

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?

like image 409
Pumpkin C Avatar asked Nov 30 '17 01:11

Pumpkin C


People also ask

What does the shape of a NumPy array mean?

The shape of an array is the number of elements in each dimension.

What is shape () in Python?

The function "shape" returns the shape of an array. The shape is a tuple of integers. These numbers denote the lengths of the corresponding array dimension.

How do you get the shape of NP Ndarray?

shape to get the dimensions of a NumPy array. Use the numpy. ndarray. shape attribute to get an array's dimensions as a tuple, where the first item is the number of rows and the second item is the number of columns.

What is Ndarray object of NumPy module?

ndarray. An array object represents a multidimensional, homogeneous array of fixed-size items. An associated data-type object describes the format of each element in the array (its byte-order, how many bytes it occupies in memory, whether it is an integer, a floating point number, or something else, etc.)


Video Answer


1 Answers

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.

like image 146
kmario23 Avatar answered Oct 06 '22 11:10

kmario23