In this article we will discuss how to count number of elements in a 1D, 2D & 3D Numpy array, also how to count number of rows & columns of a 2D numpy array and number of elements per axis in 3D numpy array.

Get the Dimensions of a Numpy array using ndarray.shape()

numpy.ndarray.shape

Python’s Numpy Module provides a function to get the dimensions of a Numpy array,

It returns the dimension of numpy array as tuple.

Let’s use this to get the shape or dimensions of a 2D & 1D numpy array i.e.

Get Dimensions of a 2D numpy array using ndarray.shape

Let’s create a 2D Numpy array i.e.

Output:

Get number of rows in this 2D numpy array i.e.

Output:

Get number of columns in this 2D numpy array,

Output:

Get total number of elements in this 2D numpy array,

Output:

Get Dimensions of a 1D numpy array using ndarray.shape

Let’s create a 1D Numpy array i.e.

Get number of elements of this 1D numpy array i.e.

Output:

Get the Dimensions of a Numpy array using numpy.shape()

Python’s Numpy module provides a function to get the number of elements in a Numpy array along axis i.e.

Args: It accepts the numpy array and also the axis along which it needs to count the elements.If axis is not passed then returns the total number of arguments.
Returns: The number of elements along the passed axis.

Let’s use this to get the shape or dimensions of a 2D & 1D numpy array i.e.

Get Dimensions of a 2D numpy array using numpy.size()

Let’s create a 2D Numpy array i.e.

Output:

Get number of rows and columns of this 2D numpy array:

Output:

Get total number of elements in this 2D numpy array:

Output:

Get Dimensions of a 3D numpy array using numpy.size()

Let’s create a 3D Numpy array i.e.

Output:

Get number of elements per axis in 3D numpy array i.e.

Output:

Get total number of elements in this 3D numpy array i.e.

Output:

Get Dimensions of a 1D numpy array using numpy.size()

Let’s create a 1D Numpy array i.e.

Get number of elements of this 1D numpy array using numpy.size() i.e.

Output:

Complete example is as follows:

Output:

Join LinkedIn Group of Python Professional Developers who wish to expand their network and share ideas.

You can also follow us On Twitter :

Click Here to Subscribe for more Articles / Tutorials like this.