NumPy ndarray.flags

The numpy.ndarray.flags attribute provides information about the memory layout of a NumPy array. It returns a dictionary-like object containing boolean flags indicating various memory properties of the array.

Syntax

</>
Copy
ndarray.flags

Parameters

This attribute does not take any parameters as it is a property of the ndarray object.

Return Value

Returns a numpy.flagsobj object containing boolean flags that provide information about the memory layout and other properties of the array.

FlagDescription
C_CONTIGUOUS (C)Data is stored in a single C-style contiguous block.
F_CONTIGUOUS (F)Data is stored in a single Fortran-style contiguous block.
OWNDATA (O)Array owns the memory it uses or shares it with another array.
WRITEABLE (W)Data can be written to.
ALIGNED (A)Data and strides are aligned appropriately for the hardware.
WRITEBACKIFCOPY (X)This array is a copy; writeback to the original array will occur upon destruction.
UPDATEIFCOPY (U)This array is a copy; writeback to the original array will occur upon destruction (deprecated).
FNCF-style contiguous or C-style contiguous.
FORCF-style contiguous and C-style contiguous.

Examples

1. Checking Memory Layout Flags of a NumPy Array

</>
Copy
import numpy as np

# Creating a simple 2x2 array
arr = np.array([[1, 2], [3, 4]])

# Accessing the flags attribute
flags_info = arr.flags
print("Flags Information:\n", flags_info)

Output:

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

The output indicates that the array is C-contiguous but not F-contiguous, owns its data, is writable, and aligned properly. The write-back flags are False, meaning no copy-on-write behavior is involved.

2. Checking Flags for a Transposed Array

</>
Copy
import numpy as np

# Creating a simple 2x3 array
arr = np.array([[1, 2, 3], [4, 5, 6]])

# Transposing the array
transposed_arr = arr.T

# Accessing the flags of the transposed array
transposed_flags = transposed_arr.flags
print("Flags of Transposed Array:\n", transposed_flags)

Output:

Flags of Transposed Array:
   C_CONTIGUOUS : False
  F_CONTIGUOUS : True
  OWNDATA : False
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False

The transposed array is Fortran-contiguous but not C-contiguous, does not own its data (as it shares memory with the original array), and remains writable.

3. Creating a Non-Writeable Array

</>
Copy
import numpy as np

# Creating a simple array
arr = np.array([10, 20, 30])

# Setting the array to be non-writeable
arr.flags.writeable = False

# Trying to modify the array
try:
    arr[0] = 100
except ValueError as e:
    print("Error:", e)

# Checking the flags
print("Array Flags:\n", arr.flags)

Output:

Error: assignment destination is read-only
Array Flags:
   C_CONTIGUOUS : True
  F_CONTIGUOUS : True
  OWNDATA : True
  WRITEABLE : False
  ALIGNED : True
  WRITEBACKIFCOPY : False

The array has been set to non-writeable, indicated by the WRITEABLE flag being False. Attempting to modify the array raises a ValueError.