ndarray - Methods and Data Type

Once you have created an array, you can check its various attributes using the inbuilt functions. Some of these are described below:

ndarray.ndim

Returns the number of array dimensions.

>>> a
array([[ 1.,  2.,  3.],
       [ 4.,  5.,  6.]])
>>> a.ndim
2
>>> x = np.array([1, 2, 3])
>>> x.ndim
1

ndarray.shape

Returns a tuple of array dimensions. It can also be used to "reshape" the array, as long as this would not require a change in the total number of elements.

>>> a.shape
(2, 3)
>>> x.shape
(3,)
>>> a.shape = (3,2)
>>> a
array([[ 1.,  2.],
       [ 3.,  4.],
       [ 5.,  6.]])

ndarray.size

Returns the number of elements in the array. This can also be calculated with np.prod(a.shape), i.e., the product of the array’s dimensions.

This content is for paid members only.

Join our membership for lifelong unlimited access to all our data science learning content and resources.

Related Downloads