9951 explained code solutions for 126 technologies


python-scipyHow do I find the size of a Python numpy array?


To find the size of a Python numpy array, you can use the numpy.size() function. This function will return the total number of elements in the array.

For example, if you have an array a declared as follows:

import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6]])

Then you can use the numpy.size() function to find the size of the array a as follows:

size_a = np.size(a)
print(size_a)

The output of this code will be:

6

The code consists of the following parts:

  1. import numpy as np: This imports the numpy library as np so that it can be used in the code.
  2. a = np.array([[1, 2, 3], [4, 5, 6]]): This creates a 2D array with the given elements.
  3. size_a = np.size(a): This calls the numpy.size() function to find the size of the array a.
  4. print(size_a): This prints the size of the array a.

For more information, please refer to the following links:

Edit this code on GitHub