NumPy Tutorial NumPy Statistics NumPy References

NumPy - Array Attributes



Array attributes reflect information that is intrinsic to the array. Accessing an array through its attributes allows to get and sometimes set intrinsic properties of the array without creating a new array. Information on most commonly used attributes are given below:

FunctionDescription
ndarray.shape Tuple of array dimensions.
ndarray.ndim Number of array dimensions.
ndarray.itemsize Length of one array element in bytes.
ndarray.size Number of elements in the array.
ndarray.dtype Data-type of the array’s elements.
ndarray.T The transposed array.
ndarray.real The real part of the array.
ndarray.imag The imaginary part of the array.
ndarray.flags Information about the memory layout of the array.

Lets discuss these attributes in detail:

ndarray.shape

The shape attribute is used to get the current shape of an array. It can also be used to reshape the array by assigning a tuple of new array dimension to it. Consider the following example.

import numpy as np
Arr = np.array([[1, 2, 3], 
                [4, 5, 6]])

print("Arr is:")
print(Arr)

print("\nShape of Arr is:", Arr.shape)

#assigning new shape to Arr
Arr.shape = (3,2)

print("\nAfter reshaping, Arr is:")
print(Arr)

The output of the above code will be:

Arr is:
[[1 2 3]
 [4 5 6]]

Shape of Arr is: (2, 3)

After reshaping, Arr is:
[[1 2]
 [3 4]
 [5 6]]

ndarray.ndim

The ndim attribute is used to get the number of array dimensions.

import numpy as np

#creating a 3-D array
Arr = np.zeros((2,3,4))

print("Arr is:")
print(Arr)

#get the number of dimension of Arr
print("\nNumber of dimension is:", Arr.ndim)

The output of the above code will be:

Arr is:
[[[0. 0. 0. 0.]
  [0. 0. 0. 0.]
  [0. 0. 0. 0.]]

 [[0. 0. 0. 0.]
  [0. 0. 0. 0.]
  [0. 0. 0. 0.]]]

Number of dimension is: 3

ndarray.itemsize

The itemsize attribute is used to get the length of the array element in bytes.

import numpy as np

#creating a 3-D array
Arr = np.array([1, 2, 3])

print("Arr is:")
print(Arr)

#length of Arr element in bytes
print("\nLength of Arr element in bytes:", Arr.itemsize)

The output of the above code will be:

Arr is:
[1 2 3]

Length of Arr element in bytes: 8

ndarray.size

The size attribute is used to get number of elements in the array. It is equivalent to product of the array's dimensions.

import numpy as np

#creating a 3-D array
Arr = np.ones((2,3,4))

print("Arr is:")
print(Arr)

#get the number of elements in Arr
print("\nNumber of elements in Arr:", Arr.size)

The output of the above code will be:

Arr is:
[[[1. 1. 1. 1.]
  [1. 1. 1. 1.]
  [1. 1. 1. 1.]]

 [[1. 1. 1. 1.]
  [1. 1. 1. 1.]
  [1. 1. 1. 1.]]]

Number of elements in Arr: 24

ndarray.dtype

The dtype attribute is used to get the data-type of the array's elements. Consider the following example.

import numpy as np
Arr = np.array([[1, 2, 3], 
                [4, 5, 6]])

print("Data-type of array is:", Arr.dtype)
print("Type of dtype is:", type(Arr.dtype))

The output of the above code will be:

Data-type of array is: int64
Type of dtype is: <class 'numpy.dtype[int64]'>

ndarray.T

The T attribute is used to get the transpose of an array.

import numpy as np
Arr = np.array([[1, 2, 3], 
                [4, 5, 6]])

print("Arr is:")
print(Arr)

print("\nTranspose of Arr is:")
print(Arr.T)

The output of the above code will be:

Arr is:
[[1 2 3]
 [4 5 6]]

Transpose of Arr is:
[[1 4]
 [2 5]
 [3 6]]

ndarray.real & ndarray.imag

The real and imag attributes are used to get the real component and imaginary component respectively of an array. The example below shows the usage of these attributes.

import numpy as np
Arr = np.array([[1+2j, 2-4j], 
                [3-6j, 4-8j]])

print("Arr is:")
print(Arr)

#real component of Arr
print("\nReal component of Arr is:")
print(Arr.real)

#Imaginary component of Arr
print("\nImaginary component of Arr is:")
print(Arr.imag)

The output of the above code will be:

Arr is:
[[1.+2.j 2.-4.j]
 [3.-6.j 4.-8.j]]

Real component of Arr is:
[[1. 2.]
 [3. 4.]]

Imaginary component of Arr is:
[[ 2. -4.]
 [-6. -8.]]

ndarray.flags

The flags attribute is used to get the information about the memory layout of the array.

FunctionDescription
C_CONTIGUOUS (C) The data is in a single, C-style contiguous segment.
F_CONTIGUOUS (F) The data is in a single, Fortran-style contiguous segment
OWNDATA (O) The array owns the memory it uses or borrows it from another object.
WRITEABLE (W) The data area can be written to. Setting this to False locks the data, making it read-only.
ALIGNED (A) The data and all elements are aligned appropriately for the hardware.
WRITEBACKIFCOPY (X) This array is a copy of some other array. The C-API function PyArray_ResolveWritebackIfCopy must be called before deallocating to the base array will be updated with the contents of this array.
UPDATEIFCOPY (U) (Deprecated, use WRITEBACKIFCOPY) This array is a copy of some other array. When this array is deallocated, the base array will be updated with the contents of this array.

Consider the following example which shows current value of the flags.

import numpy as np
Arr = np.array([[1, 2], 
                [3, 4]])

#Information about the memory layout
print(Arr.flags)

The output of the above code will be:

C_CONTIGUOUS : True
F_CONTIGUOUS : False
OWNDATA : True
WRITEABLE : True
ALIGNED : True
WRITEBACKIFCOPY : False
UPDATEIFCOPY : False