Photo by Hitesh Choudhary on Unsplash
What are the most important attributes of a Numpy array object?
In this article, we will discuss some of the most important and widely used attributes of a NumPy array object in Python. These are:
ndim: This attribute helps find the number of dimensions of a NumPy array. For example, 1 means that the array is 1D, 2 means that the array is 2D, and so on.
shape: This attribute returns the dimension of the array. It is a tuple of integers that indicates the size of your NumPy array. For example, if you create a matrix with n rows and m columns, the shape will be (n * m).
size: This attribute calculates the total number of elements present in the NumPy array.
dtype: This attribute helps to check what type of elements are stored in the NumPy array.
itemsize: This attribute helps to find the length of each element of NumPy array in bytes. For example, if you have integer values in your array, this attribute will return 8 as integer values and take 8 bits in memory.
data: This attribute is a buffer object that points to the start of the NumPy array. However, this attribute isn’t used much because we usually access the elements in the array using indices.
Code
import numpy as np
first_array = np.array([1,2,3])
second_array = np.array([[1,2,3],[4,5,6]])
print("Frst array is: :", first_array)
print("Second array is: ", second_array)
We imported the package
numpy
package.We created two NumPy arrays (one is a 1D array and the other is a 2D array) to see the outputs of the attributes that we just discussed.
Finally, we printed both arrays. The output comes out to be as shown below:
Now, let us see the ndim
attribute:
print("\nNo. of dimension of First array: ", first_array.ndim)
print("No. of dimension of Second array: ", second_array.ndim)
- We used the
ndim
attribute on both arrays and printed the dimensions. Below is the output for the above code:
Let us now use the shape
attribute:
print("\nShape of array First array: ",first_array.shape)
print("Shape of array Second array: ",second_array.shape)
- We used the
shape
attribute on both arrays and below is the output after executing the above code.
Now, let us use the size
attribute:
print("\nSize of First array: ",first_array.size)
print("Size of Second array: ",second_array.size)
- We used the
size
attribute on both arrays and below is the output generated.
Let us now use the dtype
attribute:
print("\nData type of First array: ",first_array.dtype)
print("Data type of Second array: ",second_array.dtype)
- We used the
dtype
attribute on both arrays and got the below output.
Let us now use the itemsize
attribute:
print("\nItemsize of First array: ", first_array.itemsize)
print("Itemsize of First array: ", second_array.itemsize)
- We used the
itemsize
attribute on both arrays and got the below output.
Let us use our final attribute data
on both arrays.
print("\nData of First array is: ", first_array.data)
print("Data of Second array is: ", second_array.data)
- We used the
data
attribute on both arrays and got the following output.
To learn more about numpy
and its variety of use cases, you can refer to the From Python to Numpy course which covers almost pretty much you would require in your data science journey.