Skip to main content

Command Palette

Search for a command to run...

What are the most important attributes of a Numpy array object?

Published
3 min read
What are the most important attributes of a Numpy array object?
H

Hi, I'm Harsh, a software developer at Springworks, and an Ex-TCSer.

I am a coding instructor and mentor and have been creating multiple online courses to get people comfortable learning how to code and help them get better opportunities.

I have been coding since I was 15 when I created a static website for a school project. I was given positive feedback on this project, which pushed me to major in computer science with a specialization in Artificial Intelligence.

For me, "The day is not over if I have not done any coding. I usually try to solve Competitive Programming problems, which helps me to improve my problem-solving skills. Every day I try to learn something new."

As a Software Developer for Tata Consultancy Services Limited, I have built scalable backend services using Node.js and Microsoft Azure. Apart from this, I am also an author of 6 courses at Educative.io and have been building courses on the latest technologies.

I’m familiar with various programming languages, including JavaScript, Python, and a bunch of other technical areas like System Design, Databases. I’m always adding new skills to my repertoire.

I've been Microsoft Certified in Azure Fundamentals and Azure AI Fundamentals. I am also now a Microsoft Certified Azure AI Engineer Associate.

I have delivered over 20 one-on-one sessions. If you want to talk more about coding, interview preparation, software development, or just want any career guidance, especially from the technical domain, hit me up or just connect with me at: topmate.io/harsh_jain

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.