NumPy Array Attributes

In NumPy, attributes are properties of NumPy arrays that provide information about the array's shape, size, data type, dimension, and so on.

For example, to get the dimension of an array, we can use the ndim attribute.

There are numerous attributes available in NumPy, which we'll learn below.


Common NumPy Attributes

Here are some of the commonly used NumPy attributes:

Attributes Description
ndim returns number of dimension of the array
size returns number of elements in the array
dtype returns data type of elements in the array
shape returns the size of the array in each dimension.
itemsize returns the size (in bytes) of each elements in the array
data returns the buffer containing actual elements of the array in memory

To access the Numpy attributes, we use the . notation. For example,

array1.ndim

This returns the number of dimensions in array1.


Numpy Array ndim Attribute

The ndim attribute returns the number of dimensions in the numpy array. For example,

In this example, array1.ndim returns the number of dimensions present in array1. As array1 is a 2D array, we got 2 as an output.


NumPy Array size Attribute

The size attribute returns the total number of elements in the given array.

Let's see an example.

In this example, array1.size returns the total number of elements in the array1 array, regardless of the number of dimensions.

Since these are a total of 6 elements in array1, the size attribute returns 6.


NumPy Array shape Attribute

In NumPy, the shape attribute returns a tuple of integers that gives the size of the array in each dimension. For example,

Here, array1 is a 2-D array that has 2 rows and 3 columns. So array1.shape returns the tuple (2,3) as an output.


NumPy Array dtype Attribute

We can use the dtype attribute to check the datatype of a NumPy array. For example,

In the above example, the dtype attribute returns the data type of array1.

Since array1 is an array of integers, the data type of array1 is inferred as int64 by default.

Note: To learn more about the dtype attribute to check the datatype of an array, visit NumPy Data Types.


NumPy Array itemsize Attribute

In NumPy, the itemsize attribute determines size (in bytes) of each element in the array. For example,

Output

8
4

Here,

  • array1 is an array containing 64-bit integers by default, which uses 8 bytes of memory per element. So, itemsize returns 8 as the size of each element.
  • array2 is an array of 32-bit integers, so each element in this array uses only 4 bytes of memory. So, itemsize returns 4 as the size of each element.

NumPy Array data Attribute

In NumPy, we can get a buffer containing actual elements of the array in memory using the data attribute.

In simpler terms, the data attribute is like a pointer to the memory location where the array's data is stored in the computer's memory.

Let's see an example.

Output

Data of array1 is: <memory at 0x7f746fea4a00>
Data of array2 is:  <memory at 0x7f746ff6a5a0>

Here, the data attribute returns the memory addresses of the data for array1 and array2 respectively.