Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

size of NumPy array

Is there an equivalent to the MATLAB

 size() 

command in Numpy?

In MATLAB,

>>> a = zeros(2,5)  0 0 0 0 0  0 0 0 0 0 >>> size(a)  2 5 

In Python,

>>> a = zeros((2,5)) >>>  array([[ 0.,  0.,  0.,  0.,  0.],        [ 0.,  0.,  0.,  0.,  0.]])  >>> ????? 
like image 272
abalter Avatar asked Jun 20 '12 18:06

abalter


People also ask

How do I find the size of an array in NumPy?

Use ndim attribute available with numpy array as numpy_array_name. ndim to get the number of dimensions. Alternatively, we can use shape attribute to get the size of each dimension and then use len() function for the number of dimensions.

What does size () do in NumPy?

size() function count the number of elements along a given axis.

What is size of array in Python?

Length of an array is the number of elements that are actually present in an array. You can make use of len() function to achieve this. The len() function returns an integer value that is equal to the number of elements present in that array. This returns a value of 3 which is equal to the number of array elements.

Is NumPy array fixed size?

NumPy arrays have a fixed size at creation, unlike Python lists (which can grow dynamically). Changing the size of an ndarray will create a new array and delete the original. The elements in a NumPy array are all required to be of the same data type, and thus will be the same size in memory.


2 Answers

This is called the "shape" in NumPy, and can be requested via the .shape attribute:

>>> a = zeros((2, 5)) >>> a.shape (2, 5) 

If you prefer a function, you could also use numpy.shape(a).

like image 200
Sven Marnach Avatar answered Sep 21 '22 13:09

Sven Marnach


Yes numpy has a size function, and shape and size are not quite the same.

Input

import numpy as np data = [[1, 2, 3, 4], [5, 6, 7, 8]] arrData = np.array(data)  print(data) print(arrData.size) print(arrData.shape) 

Output

[[1, 2, 3, 4], [5, 6, 7, 8]]

8 # size

(2, 4) # shape

like image 43
tcratius Avatar answered Sep 17 '22 13:09

tcratius