NumPy ndarray.strides
The ndarray.strides
attribute in NumPy provides a tuple of byte steps required to move to the next element along each dimension of an array. It is useful for understanding how the array is stored in memory and optimizing performance.
Syntax
ndarray.strides
Attributes
Attribute | Type | Description |
---|---|---|
strides | tuple of ints | Tuple of bytes indicating the step size in each dimension when traversing the array. |
Return Value
Returns a tuple of integers, where each value represents the number of bytes required to move to the next element in the corresponding dimension.
Examples
1. Understanding Strides in a 1D Array
This example demonstrates how strides work in a simple 1D NumPy array.
import numpy as np
# Creating a 1D array with dtype=int32 (4 bytes per element)
arr = np.array([10, 20, 30, 40], dtype=np.int32)
# Printing the array
print("Array:")
print(arr)
# Displaying the strides
print("Strides:", arr.strides)
Output:
Array:
[10 20 30 40]
Strides: (4,)
Since each integer (int32) takes 4 bytes, the stride value is 4
, meaning the memory offset increases by 4 bytes when moving to the next element.
2. Strides in a 2D Array
This example explores how strides change for a 2D array.
import numpy as np
# Creating a 2D array with dtype=int32 (4 bytes per element)
arr = np.array([[1, 2, 3],
[4, 5, 6]], dtype=np.int32)
# Printing the array
print("Array:")
print(arr)
# Displaying the strides
print("Strides:", arr.strides)
Output:
Array:
[[1 2 3]
[4 5 6]]
Strides: (12, 4)
The stride for the first axis (rows) is 12
bytes (since each row contains three int32 elements, each of 4 bytes: 3 × 4 = 12
). The stride for the second axis (columns) is 4
bytes, as moving between columns requires stepping over one element.
3. Strides in a Transposed Array
Strides change when the array is transposed because elements are accessed differently in memory.
import numpy as np
# Creating a 2D array with dtype=int32 (4 bytes per element)
arr = np.array([[1, 2, 3],
[4, 5, 6]], dtype=np.int32)
# Transposing the array
arr_T = arr.T
# Printing the transposed array
print("Transposed Array:")
print(arr_T)
# Displaying the strides of the transposed array
print("Strides:", arr_T.strides)
Output:
Transposed Array:
[[1 4]
[2 5]
[3 6]]
Strides: (4, 12)
After transposing, the stride for the first axis (columns) becomes 4
bytes (since stepping down moves to the next element in memory), and the stride for the second axis (rows) becomes 12
bytes, as moving across columns now requires jumping over entire rows.
4. Strides in a View of an Array
When creating a slice of an array, strides can change as NumPy optimizes memory access.
import numpy as np
# Creating a 1D array with dtype=int32 (4 bytes per element)
arr = np.array([10, 20, 30, 40, 50, 60], dtype=np.int32)
# Creating a slice that selects every second element
arr_view = arr[::2]
# Printing the sliced array
print("Sliced Array:")
print(arr_view)
# Displaying the strides of the sliced array
print("Strides:", arr_view.strides)
Output:
Sliced Array:
[10 30 50]
Strides: (8,)
Since we selected every second element, the stride is 8
bytes (jumping over two int32 elements, each 4 bytes). This is a key optimization feature of NumPy views.