Updated March 28, 2023

## Introduction to NumPy size

The NumPy size is a function that is used to calculate the total elements or values for an axis in the given array, and when we provide an input array of different data structures that we can calculate their total number of values in its axis by providing the axis 0 or 1 to the numpy size function and numpy size function is generally used to measure the total number of elements in the axis of the given array.

**Syntax:**

The basic syntax of the numpy round function is:

`numpy.size(array, axis = None)`

- numpy.size represents the function to measure the total number of elements in the axis of the given array.
- The array represents the input array in which we wanted to calculate the values in the axis.
- Axis represents the rows and columns of the elements in the given array. Axis is generally declared as 0 and 1.

### Examples of NumPy size

Given below are the examples of NumPy size:

#### Example #1

A basic example for understanding how the numpy size function works.

**Code:**

```
import numpy as np
array1=np.array([[2, 4, 6, 8], [5, 10, 15, 20]])
array2=np.array([[3, 6, 9], [5, 10, 15]])
array3=np.array([[10, 20, 30, 40, 60], [15, 10, 35, 40, 50]])
print(np.size(array1))
print(np.size(array2))
print(np.size(array3))
```

**Output:**

Here in the above example, we have declared three arrays, array1, array2 & array3, as input for the numpy size function. The input arrays consist of a set of integer values, and the objective is to find the number of values in the array’s axis.

We have printed the corresponding output of the arrays, and the first output shows us the 8 elements or integers present in the array1. The second print statement gives us the total size of the values or integers present in array 2, and the third print statement gives the total size of 10 present in array 3.

#### Example #2

In this example, we will see how we can use the axis declaration in the numpy size function, which allows us to specify a particular axis which we want to measure the number of elements in the given array.

**Code:**

```
import numpy as np
array1=np.array([[2, 4, 6, 8], [5, 10, 15, 20]])
array2=np.array([[3, 6, 9], [5, 10, 15]])
array3=np.array([[10, 20, 30, 40, 60], [15, 10, 35, 40, 50]])
print(np.size(array1,0))
print(np.size(array2,1))
print(np.size(array3,1))
```

**Output:**

In the above example, we have declared three arrays, array1, array2 & array3, as input for the numpy size function. The input arrays consist of a set of integer values, and the objective is to find the number of values in the array’s axis. Therefore, in addition to the previous example, we have declared the axis argument to the numpy size function.

We have printed the corresponding output of the arrays, and the first output shows us the 2 sets of values or elements present in the axis=0 of the array1. The second print statement gives us the total size of the values or integers present in the axis=1 of array 2; since there are three elements in axis 1, we get the output as 3.

The third print statement gives the total size of 5 present in axis 1 of array 3.

#### Example #3

Let’s try with different examples and check the output for better understanding.

**Code:**

```
import numpy as np
array1=np.array([[2, 4, 6, 8], [5, 10, 15, 20]])
array2=np.array([[3, 6, 9],[30, 40, 41]])
array3=np.array([[10, 20, 30, 40, 60], [15, 10, 35, 40, 50],[30, 40, 60,95,115]])
array4=np.array([[430, 510, 160], [15, 10, 50],[30, 40, 60]])
print(np.size(array1,0))
print(np.size(array2,1))
print(np.size(array3,0))
print(np.size(array4,1))
```

**Output:**

In the above example, we have declared four arrays, array1, array2, array3 & array3, as input for the numpy size function. The input arrays consist of a set of integer values, and the objective is to find the number of values in the array’s axis. Therefore, we have declared multiple combinations of values in the input arrays and printed their corresponding sizes by declaring a different axis argument to the numpy size function.

The first array has 2 sets of four values, and when given the axis as 0, we got two as the size of the array1 since a total of two sets of values are present in the 0^{th} axis.

The second array gives us the output of 3 since the array has a total of three elements inside two sets of values and since the axis declared is 1, we got the output as 3. Similarly, for the 3^{rd} and 4^{th} arrays, we got the respective outputs as 3 and 3 for axis 0 and axis 1.

#### Example #4

**Code:**

```
import numpy as np
array1=np.array([[2, 4, 6, 8], [5, 10, 15, 20]])
array2=np.array([[3, 6, 9],[30, 40, 41]])
array3=np.array([[10, 20, 30, 40, 60], [15, 10, 35, 40, 50],[30, 40, 60,95,115]])
array4=np.array([[430, 510, 160], [15, 10, 50],[30, 40, 60]])
print(np.size(array1,1))
print(np.size(array2,0))
print(np.size(array3,1))
print(np.size(array4,0))
```

**Output:**

Similar to the previous examples, we have declared the same 4 arrays and tried to calculate their sizes for the different axis of the input arrays. For example, the size of axis 1 in array 1 is 4, the size of axis 0 in the 2^{nd} array is 2, the size of axis 1 in the 3^{rd} array is 5, and the size of axis 0 in the 4^{th} array is 3, so like this, we can calculate the size of the given array using their axis positions.

#### Example #5

Let’s see another example where we calculate the size of arrays without using the np.size statement and axis.

**Code:**

```
import numpy as np
x = np.arange(2, 10)
y = np.arange(5, 20)
z = np.arange(2, 10,2)
print(x.size)
print(y.size)
print(z.size)
```

**Output:**

In this example, we have generated arrays x, y, and z using the numpy arange function, and we have simply used the size function to calculate the total elements present in the variables x, y, and z, and their corresponding outputs have been printed. This is an easier method where we can use the variable name along with the size keyword to find the total elements present in the array.

### Conclusion

In this article, we have seen the numpy size function in detail, using various examples to understand the numpy size function and its uses. We have also seen how to calculate the size of total elements using different arrays. We also declared the axis position and calculated the size of the array depending upon the axis positions.

### Recommended Articles

This is a guide to NumPy size. Here we discuss the introduction to NumPy size along with examples for better understanding. You may also have a look at the following articles to learn more –