Linux Hint Posted July 13, 2023 Share Posted July 13, 2023 NumPy arrays can help us to work with different data in Python. They are essentially multidimensional arrays that can store data of any type. While working with NumPy arrays we may need to get the size of the array. In Python we have numerous ways of finding the size of NumPy arrays. What is a NumPy Array? A NumPy array is a versatile data structure that allows for efficient storage and manipulation of data in a multidimensional array format. While similar in concept to Python lists, NumPy arrays offer several advantages. Firstly, they have superior performance in mathematical operations compared to Python lists. NumPy arrays are designed to be memory-efficient, ensuring optimized memory usage. NumPy provides a range of convenient functions that facilitate working with data effectively. How to Get the Size of a One-Dimensional NumPy Array The size attribute in Python can give us the size of one-dimensional NumPy arrays. Below Python code gets the size of a one-dimensional NumPy array: import numpy as np array = np.array([1, 2, 3, 4, 5]) size = array.size print(size) This code will print the number 5, which is the size of the array. How to Get the Size of a Multi-Dimensional NumPy Array The shape attribute in Python can get a multidimensional NumPy array size. The shape attribute describes the size or structure of the array in terms of its dimensions, such as the number of rows, columns, and other axes. Below Python code gets the size of a two-dimensional NumPy array: import numpy as np array = np.array([[1, 2, 3], [4, 5, 6]]) shape = array.shape print(shape) This code will print the (2, 3), which represents the dimensions of the array. The first element describes the total rows and the second describes the total column in the array. How to Get Size of NumPy Array Using len() Function In addition to the size and shape attributes, there are a few other ways to get the size of a NumPy array. The len() function is one of those that give us the NumPy array size. This function outputs the length of a list, and it also tells the total NumPy array elements. For example, the following code gets the size of a NumPy array using the len() function: import numpy as np array = np.array([1, 2, 3, 4, 5]) size = len(array) print(size) This code will print the number 5, which is the size of the array. How to Get Size of NumPy Array Using prod() Function The second method using which we can get NumPy array size is the prod() function. The prod() function returns the product of the elements in a list, and it can also be used to get the size of a NumPy array. Below MATLAB code gets the NumPy array size using the prod() function: import numpy as np array = np.array([1, 2, 3, 4, 5]) size = np.prod(array.shape) print(size) This code will also print the number 5, which is the size of the array. Conclusion NumPy arrays provide a data structure for multidimensional data in Python. The size of a NumPy array can be obtained using the size attribute for one-dimensional arrays and the shape attribute for multi-dimensional arrays. Additionally, the len() function and the prod() function can be used to determine the size of a NumPy array. View the full article Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.