11-19-2020, 02:46 AM
np.shape()
This tutorial explains NumPy’s function.
Return the shape of an array or array_like object.
Argument |
Data Type |
Description
|
array_like |
NumPy array or Python list for which the shape should be returned. If it is a NumPy array, it returns the attribute. If it is a Python list, it returns a tuple of integer values defining the number of elements in each dimension if you would’ve created a NumPy array from it.
Return Value: — a tuple of integers that are set to the lengths of the corresponding array dimensions.
Examples
The straightforward example is when applied to a NumPy array:
You import the NumPy library and create a two-dimensional array from a list of lists. If you pass the NumPy array into the shape function, it returns a tuple with two values (=dimensions). Each dimension stores the number of elements in this dimension (=axis). As it is a 2×2 quadratic matrix, the result is (2,2).
The following shape is another example of a multi-dimensional array:
The shape is now with two rows and four columns.
np.shape() vs array.shape
Note that the result of and is the same if is a NumPy array. If isn’t a NumPy array but a list, you cannot use as lists don’t have the shape attribute. Let’s have a look at this example:
The function returns the same shape tuple—even if you pass a nested list into the function instead of a NumPy array.
But if you try to access the list.shape attribute, NumPy throws the following error:
So, the difference between and is that the former can be used for all kinds of array_like objects while the latter can only be used for NumPy arrays with the attribute.
Do you want to become a NumPy master? Check out our interactive puzzle book Coffee Break NumPy and boost your data science skills! (Amazon link opens in new tab.)
![[Image: Cover_Coffee_Break_NumPy-200x300.jpg]](https://blog.finxter.com/wp-content/uploads/2019/04/Cover_Coffee_Break_NumPy-200x300.jpg)
References
The post np.shape() first appeared on Finxter.
This tutorial explains NumPy’s
Code:
shape()Code:
numpy.shape(a)Return the shape of an array or array_like object
Code:
aArgument |
Data Type |
Description
Code:
aarray_like |
NumPy array or Python list for which the shape should be returned. If it is a NumPy array, it returns the attribute
Code:
a.shapeReturn Value:
Code:
shapeExamples
The straightforward example is when applied to a NumPy array:
Code:
>>> import numpy as np
>>> a = np.array([[1, 2], [3, 4]])
>>> np.shape(a)
(2, 2)You import the NumPy library and create a two-dimensional array from a list of lists. If you pass the NumPy array into the shape function, it returns a tuple with two values (=dimensions). Each dimension stores the number of elements in this dimension (=axis). As it is a 2×2 quadratic matrix, the result is (2,2).
The following shape is another example of a multi-dimensional array:
Code:
>>> b = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
>>> b
array([[1, 2, 3, 4], [5, 6, 7, 8]])
>>> b.shape
(2, 4)
>>> np.shape(b)
(2, 4)The shape is now
Code:
(2, 4)np.shape() vs array.shape
Note that the result of
Code:
np.shape(b)Code:
b.shapeCode:
bCode:
bCode:
b.shapeCode:
>>> b = [[1, 2, 3, 4], [5, 6, 7, 8]]
>>> np.shape(b)
(2, 4)The
Code:
np.shape()But if you try to access the list.shape attribute, NumPy throws the following error:
Code:
>>> b.shape
Traceback (most recent call last): File "<pyshell#9>", line 1, in <module> b.shape
AttributeError: 'list' object has no attribute 'shape'So, the difference between
Code:
np.shape()Code:
array.shapeCode:
shapeDo you want to become a NumPy master? Check out our interactive puzzle book Coffee Break NumPy and boost your data science skills! (Amazon link opens in new tab.)
![[Image: Cover_Coffee_Break_NumPy-200x300.jpg]](https://blog.finxter.com/wp-content/uploads/2019/04/Cover_Coffee_Break_NumPy-200x300.jpg)
References
- Implementation: https://github.com/numpy/numpy/blob/mast...1926-L1969
The post np.shape() first appeared on Finxter.

