shape vs len for numpy array
np size vs len
numpy array size
shape of tuple python
numpy shape rows columns
numpy size of 2d array
list shape python
numpy array dimensions
Is there a difference (in performance for example) when comparing
len? Consider the following example:
In : import numpy as np In : a = np.array([1,2,3,4]) In : a.shape Out: (4,) In : len(a) Out: 4
Quick runtime comparison suggests that there's no difference:
In : a = np.random.randint(0,10000, size=1000000) In : %time a.shape CPU times: user 6 µs, sys: 2 µs, total: 8 µs Wall time: 13.1 µs Out: (1000000,) In : %time len(a) CPU times: user 5 µs, sys: 1 µs, total: 6 µs Wall time: 9.06 µs Out: 1000000
So, what is the difference and which one is more pythonic? (I guess using
I wouldn't worry about performance here - any differences should only be very marginal.
I'd say the more pythonic alternative is probably the one which matches your needs more closely:
a.shape may contain more information than
len(a) since it contains the size along all axes whereas
len only returns the size along the first axis:
>>> a = np.array([[1,2,3,4], [1,2,3,4]]) >>> len(a) 2 >>> a.shape (2L, 4L)
If you actually happen to work with one-dimensional arrays only, than I'd personally favour using
len(a) in case you explicitly need the array's size.
python - shape vs len for numpy array, Is there a difference (in performance for example) when comparing shape and len ? Consider the following example: In : import numpy as np In : a� The shape of an array is the number of elements in each dimension. Get the Shape of an Array NumPy arrays have an attribute called shape that returns a tuple with each index having the number of corresponding elements.
From the source code, it looks like shape basically uses
@property def shape(self) -> Tuple[int, int]: return len(self.index), len(self.columns)
def __len__(self) -> int: return len(self.index)
Calling shape will attempt to run both dim calcs. So maybe
df.shape + df.shape is slower than
len(df.index) + len(df.columns). Still, performance-wise, the difference should be negligible except for a giant giant 2D dataframe.
So in line with the previous answers,
df.shape is good if you need both dimensions, for a single dimension,
len() seems more appropriate conceptually.
Looking at property vs method answers, it all points to usability and readability of code. So again, in your case, I would say if you want information about the whole dataframe just to check or for example to pass the shape tuple to a function, use
shape. For a single column, including index (i.e. the rows of a df), use
Difference between len and size, size comes from numpy (on which pandas is based). It gives you the These arrays, in turn, have their own shape and size. But len() will return 5, because the number of elements in higher order list (or 1st dimension is 5). To get the number of dimensions, shape (size of each dimension) and size (number of all elements) of NumPy array, use attributes ndim, shape, and size of numpy.ndarray. The built-in function len() returns the size of the first dimension. Number of dimensions of numpy.ndarray: ndim; Shape of numpy.ndarray: shape
For 1D case, both len and shape will produce same result. For other case, I shape will provide more information. It depends on program to program in which will provide you better performance. I suggest you to not to worry much about performance.
NumPy: Get the number of dimensions, shape, and size of ndarray , Size of the first dimension of numpy. len() is the built-in function that returns the number of elements in a list or the number of characters in a string. ndarray , len() returns the size of the first dimension. Equivalent to shape and also equal to size only for one-dimensional arrays. The Python Numpy module has a shape function, which helps us to find the shape or size of an array or matrix. Apart from this, the Python Numpy module has reshape, resize, transpose, swapaxes, flatten, ravel, and squeeze functions to alter the matrix of an array to the required shape.
import numpy as np x = np.linspace(1, 10, 10).reshape((5, 2)) print(x) print(x.size) print(len(x))
gives the following output:
[[ 1. 2.] [ 3. 4.] [ 5. 6.] [ 7. 8.] [ 9. 10.]] 10 5
numpy.ndarray.size - Numpy and Scipy Documentation, The number of dimensions is the rank of the array; the shape of an array is a tuple of This means that an arbitrary integer array of length "n" in numpy needs. The number of dimensions is the rank of the array; the shape of an array is a tuple of integers giving the size of the array along each dimension. The Python core library provided Lists. A list is the Python equivalent of an array, but is resizeable and can contain elements of different types.
Python Lists vs. Numpy Arrays, Tuple of array dimensions. The shape property is usually used to get the current shape of an array, but may also be used to reshape the array� numpy.shape¶ numpy.shape (a) [source] ¶ Return the shape of an array. Parameters a array_like. Input array. Returns shape tuple of ints. The elements of the shape tuple give the lengths of the corresponding array dimensions.
numpy.ndarray.shape — NumPy v1.19 Manual, For a matrix with n rows and m columns, shape will be (n,m). The length of the shape tuple is therefore the rank, or� The N-dimensional array (ndarray)¶An ndarray is a (usually fixed-size) multidimensional container of items of the same type and size. The number of dimensions and items in an array is defined by its shape, which is a tuple of N non-negative integers that specify the sizes of each dimension.
- Just for completeness:
a.shape == len(a).
- For completeness,
a.sizeis also an option. Though personally I prefer
len(a)just in case
aisn't a numpy array, which does happens sometimes when you are not careful.
len(np.array([0,2])) , type(np.array([0,2]).shape).
lenreturns an int,
shapereturns a tuple of ints. This is important if actually using results in code rather than inspecting by eye