Create a NumPy Array
Import NumPy and create a NumPy array.
Size of an Array
NumPy array has an attribute called size that tells you the total number of elements in the array.
You can check the size of a NumPy array using the ‘.size’ attribute. This will return the total number of elements present in the array:
Data Types of Array
You can check the type of data present in the NumPy array using the ‘.dtype’ attribute. The data type of array ‘arr’ is ‘int’ (integer). Here, ‘32’ is related to memory allocation.
Note: If you have a 64-bit computer, dtype might be displayed as int64, and if you have 32-bit, it might be displayed as int32
Shape of an Array
An array has an attribute called shape that tells you the number of items along each axis.
If we have an array with two axes, the shape attribute will tell you the total number of rows and columns in that array.
The shape attribute of NumPy returns a tuple of integers that represents the number of items along each axis.
Consider the given array:
You can check the shape of a NumPy array using the method ‘.shape’. In array ‘arr’, there are 4 rows and 3 columns.
Dimension of an Array
Consider the given array:
An array has an attribute called dimension that tells you the number of axes in the array.
You can check the number of dimensions of a NumPy array using the ‘.ndim’ attribute. The array ‘arr’ is 2 dimensional (i.e., the array has two axes).
type() function and dtype method
Everything in Python is an object.
Suppose you have an object. If you want to know what type of object it is, you will use the type() function to learn about it.
Now you know the object is a NumPy ndarray. If you want to know the type of data present in that array, you will use the attribute ‘.dtype’. This is also called the type of NumPy array.
Caution: dtype method on Python Lists
This is to be taken care that Python Lists don’t support the ‘.dtype’ attribute or any other methods supported by a NumPy array.