NumPy provides different attributes for managing and getting information about arrays.

NumPy arrays have 17 main attributes.

Where NumPy arrays have 15 primary attributes. Methods like tolist() and view() are often used alongside attributes but are not strictly attributes.

NumPy arrays have 17 main attributes are:


1) ndim attribute

ndim stands for number of dimensions. This attribute returns the number of dimensions of the array.

Use: It helps us to know whether array object (ndarray) is 1D, 2D, or higher-dimensional.

Syntax:

array_name.ndim

Example:

import numpy as np

arr = np.array([[13, 22, 43], [34, 45, 65]])

print(arr.ndim)

Output:

2  #2 is the dimensions of the ndarray

 

 2) shape attribute

shape attribute returns the size of the array along with each dimension.

Use: shape is used to understand the structure and size of the array.

Syntax:

array_name.shape

If array is 1D

Example:

import numpy as np

arr = np.array([13, 22, 43, 34, 45, 56])

print(arr.shape)

Output:

(6,) #return columns / total elements

 If array is 2D

Example:

import numpy as np

arr = np.array([[23, 34, 45], [23, 45, 56], [12, 12, 23]])

print(arr.shape)

Output:

(3, 3) # number of rows and columns

 

3) size attribute

Returns the total number of elements in the array.

Use: To find how many data points the array contains.

Syntax:

array_name.size

Example:

import numpy as np

arr = np.array([[23, 34, 45], [23, 45, 56], [12, 12, 23]])

print(arr.size)

Output:

9

 

4) dtype attribute

dtype returns the data type of the array's elements.

Use: To confirm the type of data stored in the array.

Syntax:

array_name.dtype

Example:

import numpy as np

arr = np.array([[23, 34, 45], [23, 45, 56]])

print(arr.dtype)

Output:

int64 #it means every element in this array have data type int64

 

5) itemsize attribute

returns the size of each element in a NumPy array in bytes. It indicates the memory required to store a single array element. For example, if the array contains integers of 4 bytes each, itemsize will return 4

Syntax:

array_name.itemsize

Example:

import numpy as np

arr = np.array([1, 2, 3], dtype=np.int32)

print("Item size:", arr.itemsize, "bytes")

Output:

Item size: 4 bytes

 

6) nbytes attribute

nbytes return the total memory (in bytes) used by the array's elements.

Use: To calculate the total memory used by the array.

Syntax:

array_name.nbytes

Example:

import numpy as np

arr = np.array([[1, 2], [3, 4]], dtype=np.float64)

print("Total memory used:", arr.nbytes, "bytes")

Output:

Total memory used: 32 bytes

Explanation:

The data type is specified as np.float64, meaning each element is stored as a 64-bit (8-byte) floating-point number.

itemsize = 8 bytes (since dtype=np.float64).

Total memory used = 8 bytes × 4 = 32 bytes.


 

7) T attribute

This is a simple transposed of an array. Means it changes rows to columns or columns to rows as we do in math matrices.

Use: To swap the dimensions of a 2D or higher array.

Syntax:

array_name.T

Example:

import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])

print("Original array:\n", arr)

print("Transposed array:\n", arr.T)

Output:

Original array:
 [[1 2 3]
 [4 5 6]]

Transposed array:
 [[1 4]
 [2 5]
 [3 6]]

 

8) data attribute

data attributes return a object or that object holds the raw data of the array in the computer memory. That object is not hold array values but it hold the pointer data where the values are in the system memory.

Syntax:

array_name.data

Example:

arr = np.array([10, 20, 30])

print("Array:", arr)

print("Data buffer address:", arr.data)

Output:

Data buffer address: <memory at 0x78dacc456f80>

 

9) strides attribute

The strides attribute in NumPy tells us how many bytes you need to move in memory to go from one element to the next element along each dimension of an array.

Syntax:

array_name.strides

Example:

import numpy as np

# Create a 2D array
arr = np.array([[0, 1, 2], [3, 4, 5]], dtype=np.int32)  # int32 means 4 bytes per element
print("Array:\n", arr)
print("Strides:", arr.strides)

Output:

Data buffer address: <memory at 0x78dacc456f80>

Explanation:

  • 12 bytes: Move to the next row (3 elements × 4 bytes each).
  • 4 bytes: Move to the next column (1 element × 4 bytes).

 

10) flags attribute

The flags attribute of a NumPy array returns the information about the memory layout and properties of the array. It is an object that contains multiple attributes, that give characteristics of the array.

Syntax:

array_name.flags

Example:

import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])

print("Flags:\n", arr.flags)

Output:

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

Where flags meanings are:

C_CONTIGUOUS: Data is stored row by row in memory.

F_CONTIGUOUS: Data is stored column by column in memory.

OWNDATA: Array owns its memory, not a view of another.

WRITEABLE: Array can be modified.

ALIGNED: Data is properly aligned for hardware.

UPDATEIFCOPY: Temporary copy to update the original (deprecated).


 

11) real attribute

It is helpful when working with arrays containing complex numbers and we want to get only real parts from the elements.

Syntax:

array_name.real

Example:

import numpy as np

arr = np.array([1 + 2j, 3 + 4j, 5 + 6j])

print("Original array:", arr)

print("Real part:", arr.real)

Output:

Original array: [1.+2.j 3.+4.j 5.+6.j]

Real part: [1. 3. 5.]

 

12) imag attribute

Same as the real attribute, It is helpful when working with arrays containing complex numbers and we want to get only imaginary parts from the elements.

Syntax:

array_name.imag

Example:

import numpy as np

arr = np.array([1 + 2j, 3 + 4j, 5 + 6j])

print("Original array:", arr)

print("imag part:", arr.imag)

Output:

Original array: [1.+2.j 3.+4.j 5.+6.j]

imag part: [2. 4. 6.]

 

13) base attribute

The base attribute tells you if the array is sharing data with another array (a view) or if it has its own separate data.

Use:

  • If base is None, the array owns its data.
  • If base is not None, it points to the original array it is sharing data with.

Syntax:

array_name.base

Example:

import numpy as np

original = np.array([1, 2, 3])

view = original[1:]

# Check the base attribute
print("Original array base:", original.base)  # None (it owns the data)
print("View base:", view.base)                # Shows the original array

Output:

Original array base: None

View base: [1 2 3]

 

14) ctypes attribute

ctypes attribute returns a object that helps us to work with C-style memory. C-style memory refers to the way where memory is organized and accessed in the C programming language.

In Simple words:

  • It gives you access to the raw memory address of the array's data.
  • Useful for advanced use cases like working with external C libraries.

Syntax:

array_name.ctypes

Example:

import numpy as np

arr = np.array([1, 2, 3, 4], dtype=np.int32)

data_pointer = arr.ctypes.data

print("Memory address:", data_pointer)

Output:

Memory address: 99191475500320

 

15) flat attribute

flat attribute simplifies working with elements of an array when you don’t need to worry about its shape (whether it’s 1D, 2D, or higher).

The flat attribute in NumPy provides a 1D iterator over a multi-dimensional array, allowing us to easily iterate over all the elements and modify all elements of the array, regardless of its shape.

Syntax:

array_name.flat

Example:

import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])

for element in arr.flat:
    print(element)

Output:

1
2
3
4
5
6

Modifying elements with flat:

 Example:

import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])

arr.flat[0] = 10

print(arr)

Output:

[[10  2  3]
 [ 4  5  6]]

 

16) tolist() (method)

tolist() is used when we want to convert numpy array to Python list.

Syntax:

array_name.tolist()

Example:

import numpy as np

arr = np.array([1, 2, 3, 4, 5])

lst = arr.tolist()

print(lst)
print(type(lst))

Output:

[1, 2, 3, 4, 5]
<class 'list'>

 

17) view() (method)

Returns a new view of the array with the same data.

Use: To create an array that shares the same data.

Syntax:

array_name.view()

Example:

import numpy as np

arr = np.array([1, 2, 3, 4, 5])  # Original array
view_arr = arr.view()  # Create a view of the original array

print(arr)  # [10, 2, 3, 4, 5]
print(view_arr)  # [10, 2, 3, 4, 5]

Output:

[1 2 3 4 5]
[1 2 3 4 5]

 

Leave a comment

You must be logged in to post a comment.

0 Comments