Get Size of Numpy Array
NumPy is a python package. To use this package or to use any function of the numpy library, we have to import the library by using “import numpy as np”, or we can simply use “import numpy” without defining the function name. If we imported the library with a function name, use a function name with size() function, i.e., np.size(). If we imported numpy without a function name, every time we call any method of the numpy library, we need to use numpy with that function, i.e., numpy.size().
Array_1: In arrays, we stored the elements of the same data type. Array_1 is the input array. We can also store elements having different data types in an array, but that is not a good practice. Python allows the storage of different data types in one array variable.
Axis: The parameter “axis” is optional; if we want to count along the rows and columns, then this parameter is used. And if it is not used, by default, it gives the total number of the array elements.
It gives the size of the array, and it is an integer value because the size of an array can never be in a floating point.
- len() function: The len() method gets the size of a 1D array. If we try to find the size of two dimensional or n-dimensional array, it will only return the number of rows, not the total number of elements.
- size() function: This method can find the size of any type of array (n-dimensional array). It gives the particular size of the array but to use this method; we need to import the numpy library. The size() method multiplies the number of rows and columns to find the total size, i.e., size=rows*columns.
- shape() function: It is used to get the number of rows and columns of arrays, but in the case of a one-dimensional array, it returns the total number of elements that is the size of the array. By using this function, we can acquire the shape of any dimensional array.
Using len() Function
We will observe how we utilize the len() method to get the size of a one-dimensional array. With the help of the len() function, we will acquire the length (all elements) of the defined array.
In this program, import the numpy library with function name np. Initialized one-dimensional array having the integer values and stored in a variable, say “array_0”. Then with the help of the print() method, a message is presented on the console. And with another print() statement, display the size of the required array by calling the len() method. Inside the parenthesis, give the array name whose size we want to find. The total elements of the defined array can be obtained by the use of the len() function.
Using size() Function
In this case, we will get the size of the 2D array by using the size() function. Here the size of the array means the total values in the array.
Integrating the numpy library to use the NumPy array. Declare the two-dimensional array. This 2D array consists of different integer values. Print the message on the output screen with the print() function. Then call the np.size() function to get the size of the 2D array. We can also acquire the size of rows and columns by utilizing the size() method and print them by the use of a print statement. The size() function accepts two attributes for finding the number of rows and columns in a 2D array. The argument “arr” is the array whose size we will find, and 0 means rows; it will get the size of rows. And 1 means columns; it would acquire the no. of columns in the array.
The outcome shows the size of the array, which is 8, the number of rows is 2, and the number of columns is 4. If we multiply rows and columns, we can get the size of the array.
Using shape() Method
The shape() method is also used to find the size of the two-dimensional array.
In the first statement, incorporate the numpy module as np. Then we define the 2D array and store it in the “Array” variable; later, we will find the size of this array. Print the message on the screen by invoking the print() method. Then use np.shape() to get the size of the required 2D array. Here the size means the number of rows and columns of the array.
Using len(), size(), and shape() Methods
In this program, we are going to apply the len(), size(), and shape() methods to obtain the size of the NumPy array.
The above code will get the size of the 2D array with three different methods, len(), size(), and shape(). The len() method can find the length of the 1D array, but when it comes to a 2D array or n-dimensional, the len() function only gives the number of rows, the size() method gives the total number of elements, and the shape() function gives the number of rows and columns.
The numpy library is imported. The elements of the 2D array will be declared in the next statement and will be saved in the “array_1” variable. Then print the message of finding the size by len() method; we have to call the print() function. Further, utilize the size() function and display the size of the 2D array. In the end, invoke the shape() method to get the size of the array. The print statement prints the message along with the size of a 2D array.
Now we can see how the three different approaches affect the output of the same 2D array. The len() method returns the no. of rows of a 2D array. The array’s exact size can be found by using size(). The shape() function returns the array’s rows and columns.
In this guide, we have explained in detail what the size of an array means and how we can get the size of a NumPy array. With the help of three different functions (len(), size(), and shape()), we can easily get the size of an array. There is a clear difference between these techniques that are explained in the article. We have illustrated each method with the help of programs and acquired the size of both one and two-dimensional arrays. The article concludes with the last example of getting the size of the NumPy array by using all three methods.