In this article, we will be discussing how to count several elements in 1D, 2D, and 3D Numpy array. Moreover, we will be discussing the counting of rows and columns in a 2D array and the number of elements per axis in a 3D Numpy array.
Let’s get started!
Get the Dimensions of a Numpy array using ndarray.shape()
NumPy.ndarray.shape
This module is used to get a current shape of an array, but it is also used to reshaping the array in place by assigning a tuple of arrays dimensions to it. The function is:
ndarray.shape
We will use this function for determining the dimensions of the 1D and 2D array.
Get Dimensions of a 2D NumPy array using ndarray.shape:
Let us start with a 2D Numpy array.
2D Numpy Array [[11 12 13 11] [21 22 23 24] [31 32 33 34]]
Get the number of rows in this 2D NumPy array:
Code:
numOfRows = arr2D.shape[0] print('Number of Rows : ', numOfRows)
Number of Rows : 3
Get a number of columns in this 2D NumPy array:
Code:
numOfColumns = arr2D.shape[1] print('Number of Columns : ', numOfColumns)
Number of Columns: 4
Get the total number of elements in this 2D NumPy array:
Code:
print('Total Number of elements in 2D Numpy array : ', arr2D.shape[0] * arr2D.shape[1])
Total Number of elements in 2D Numpy array: 12
Get Dimensions of a 1D NumPy array using ndarray.shape
Now, we will work on a 1D NumPy array.
Code:
arr = np.array([4, 5, 6, 7, 8, 9, 10, 11])
Shape of 1D numpy array : (8,) length of 1D numpy array : 8
Get the Dimensions of a Numpy array using NumPy.shape()
Now, we will see the module which provides a function to get the number of elements in a Numpy array along the axis.
numpy.size(arr, axis=None)
We will use this module for getting the dimensions of a 2D and 1D Numpy array.
Get Dimensions of a 2D numpy array using numpy.size()
We will begin with a 2D Numpy array.
Code:
arr2D = np.array([[11 ,12,13,11], [21, 22, 23, 24], [31,32,33,34]]) print('2D Numpy Array') print(arr2D)
Output:
Get a number of rows and columns of this 2D NumPy array:
Code:
numOfRows = np.size(arr2D, 0) # get number of columns in 2D numpy array numOfColumns = np.size(arr2D, 1) print('Number of Rows : ', numOfRows) print('Number of Columns : ', numOfColumns)
Number of Rows : 3 Number of Columns: 4
Get a total number of elements in this 2D NumPy array:
Code:
print('Total Number of elements in 2D Numpy array : ', np.size(arr2D))
Output:
Total Number of elements in 2D Numpy array: 12
Get Dimensions of a 3D NumPy array using numpy.size()
Now, we will be working on the 3D Numpy array.
Code:
arr3D = np.array([ [[11, 12, 13, 11], [21, 22, 23, 24], [31, 32, 33, 34]], [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]] ]) print(arr3D)
[[[11 12 13 11] [21 22 23 24] [31 32 33 34]] [[ 1 1 1 1] [ 2 2 2 2] [ 3 3 3 3]]]
Get a number of elements per axis in 3D NumPy array:
Code:
print('Axis 0 size : ', np.size(arr3D, 0)) print('Axis 1 size : ', np.size(arr3D, 1)) print('Axis 2 size : ', np.size(arr3D, 2))
Output:
Axis 0 size : 2 Axis 1 size : 3 Axis 2 size : 4
Get the total number of elements in this 3D NumPy array:
Code:
print(‘Total Number of elements in 3D Numpy array : ‘, np.size(arr3D))
Output:
Total Number of elements in 3D Numpy array : 24
Get Dimensions of a 1D NumPy array using numpy.size()
Let us create a 1D array.
Code:
arr = np.array([4, 5, 6, 7, 8, 9, 10, 11]) print('Length of 1D numpy array : ', np.size(arr))
Output:
Length of 1D numpy array : 8
import numpy as np def main(): print('**** Get Dimensions of a 2D numpy array using ndarray.shape ****') # Create a 2D Numpy array list of list arr2D = np.array([[11 ,12,13,11], [21, 22, 23, 24], [31,32,33,34]]) print('2D Numpy Array') print(arr2D) # get number of rows in 2D numpy array numOfRows = arr2D.shape[0] # get number of columns in 2D numpy array numOfColumns = arr2D.shape[1] print('Number of Rows : ', numOfRows) print('Number of Columns : ', numOfColumns) print('Total Number of elements in 2D Numpy array : ', arr2D.shape[0] * arr2D.shape[1]) print('**** Get Dimensions of a 1D numpy array using ndarray.shape ****') # Create a Numpy array from list of numbers arr = np.array([4, 5, 6, 7, 8, 9, 10, 11]) print('Original Array : ', arr) print('Shape of 1D numpy array : ', arr.shape) print('length of 1D numpy array : ', arr.shape[0]) print('**** Get Dimensions of a 2D numpy array using np.size() ****') # Create a 2D Numpy array list of list arr2D = np.array([[11, 12, 13, 11], [21, 22, 23, 24], [31, 32, 33, 34]]) print('2D Numpy Array') print(arr2D) # get number of rows in 2D numpy array numOfRows = np.size(arr2D, 0) # get number of columns in 2D numpy array numOfColumns = np.size(arr2D, 1) print('Number of Rows : ', numOfRows) print('Number of Columns : ', numOfColumns) print('Total Number of elements in 2D Numpy array : ', np.size(arr2D)) print('**** Get Dimensions of a 3D numpy array using np.size() ****') # Create a 3D Numpy array list of list of list arr3D = np.array([ [[11, 12, 13, 11], [21, 22, 23, 24], [31, 32, 33, 34]], [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]] ]) print('3D Numpy Array') print(arr3D) print('Axis 0 size : ', np.size(arr3D, 0)) print('Axis 1 size : ', np.size(arr3D, 1)) print('Axis 2 size : ', np.size(arr3D, 2)) print('Total Number of elements in 3D Numpy array : ', np.size(arr3D)) print('Dimension by axis : ', arr3D.shape) print('**** Get Dimensions of a 1D numpy array using numpy.size() ****') # Create a Numpy array from list of numbers arr = np.array([4, 5, 6, 7, 8, 9, 10, 11]) print('Original Array : ', arr) print('Length of 1D numpy array : ', np.size(arr)) if __name__ == '__main__': main()
**** Get Dimensions of a 2D numpy array using ndarray.shape **** 2D Numpy Array [[11 12 13 11] [21 22 23 24] [31 32 33 34]] Number of Rows : 3 Number of Columns : 4 Total Number of elements in 2D Numpy array : 12 **** Get Dimensions of a 1D numpy array using ndarray.shape **** Original Array : [ 4 5 6 7 8 9 10 11] Shape of 1D numpy array : (8,) length of 1D numpy array : 8 **** Get Dimensions of a 2D numpy array using np.size() **** 2D Numpy Array [[11 12 13 11] [21 22 23 24] [31 32 33 34]] Number of Rows : 3 Number of Columns : 4 Total Number of elements in 2D Numpy array : 12 **** Get Dimensions of a 3D numpy array using np.size() **** 3D Numpy Array [[[11 12 13 11] [21 22 23 24] [31 32 33 34]] [[ 1 1 1 1] [ 2 2 2 2] [ 3 3 3 3]]] Axis 0 size : 2 Axis 1 size : 3 Axis 2 size : 4 Total Number of elements in 3D Numpy array : 24 Dimension by axis : (2, 3, 4) **** Get Dimensions of a 1D numpy array using numpy.size() **** Original Array : [ 4 5 6 7 8 9 10 11] Length of 1D numpy array : 8
I hope you understood this article well.