T or transpose() swaps axes. Transposes often return views—same memory, different strides—so writes through transposed arrays affect the original.
2D transpose
import numpy as np
a = np.arange(6).reshape(2, 3)
print(a.T)
print(a.T.shape)
Higher dimensions
arr.transpose(2, 0, 1) reorders axes explicitly—common in image batches (NHWC ↔ NCHW).
View detection
import numpy as np
a = np.arange(4)
b = a.reshape(2, 2)
c = b.T
c[0, 0] = 999
print(a) # mutated
Important interview questions and answers
- Q: T vs transpose?
A: Property .T is shorthand; transpose() accepts axis tuple for nD. - Q: When is transpose a copy?
A: When memory layout requires non-contiguous reorder that can't be strided as view.
Self-check
- Transpose a 3×4 matrix—new shape?
- Why can transposed view mutate original?
Tip: Check np.shares_memory when unsure about aliasing.
Interview prep
- T effect?
Swaps rows and columns—often a view.
- Mutation risk?
Editing transpose can change original array.