590 words
3 minutes
NumPy Array Operations

II. NumPy Array Operations (数组操作)
Array operations (数组操作) let you change an array's shape (形状), dimensions (维度), and structure — without touching the underlying data values. These are the core tools for preparing data before computation.
1. reshape() — Change Shape Without Copying (改变形状)
Core idea: Returns a new view of the same data with a different shape. Total elements must stay the same.
import numpy as np
a = np.arange(12) # [0, 1, 2, ..., 11]b = a.reshape(3, 4) # 3 rows × 4 colsc = a.reshape(2, 3, 2) # 3-D: 2×3×2
# Use -1 to let NumPy infer one dimensiond = a.reshape(4, -1) # → shape (4, 3)Note:
reshape returns a view — modifying b will also change a. Use .copy() if you need independence.2. resize() — Reshape In-Place (原地改变形状)
Core idea: Like reshape, but modifies the array in-place and can change total element count by repeating or truncating data.
a = np.array([1, 2, 3, 4])a.resize(2, 3) # repeats values to fill: [[1,2,3],[4,1,2]]print(a)Note: resize() modifies the original array permanently — use with caution.
3. flatten() — Collapse to 1-D (展平为一维)
Core idea: Always returns a copy as a flat 1-D array.
b = np.array([[1, 2], [3, 4]])print(b.flatten()) # [1 2 3 4]print(b.ravel()) # [1 2 3 4] — same result, but returns a VIEW| Method | Returns | Modifies original? |
|---|---|---|
flatten() | Copy | No |
ravel() | View (usually) | Yes (if view) |
4. transpose() — Swap Axes (转置)
Core idea: Swap rows and columns (or any axes in higher dimensions). Shortcut: .T
a = np.array([[1, 2, 3], [4, 5, 6]]) # shape (2, 3)
print(a.T) # shape (3, 2)print(a.transpose()) # same as a.T
# For 3-D: specify axis orderc = np.ones((2, 3, 4))c.transpose(2, 0, 1) # new shape: (4, 2, 3)5. concatenate() — Join Arrays (拼接数组)
Core idea: Join a sequence of arrays along an existing axis (轴).
a = np.array([[1, 2], [3, 4]])b = np.array([[5, 6]])
# axis=0: stack rows (垂直拼接)np.concatenate([a, b], axis=0) # shape (3, 2)
# axis=1: stack columns (水平拼接)c = np.array([[7], [8]])np.concatenate([a, c], axis=1) # shape (2, 3)Note: Convenience wrappers:
np.vstack() (vertical / axis=0) and np.hstack() (horizontal / axis=1).6. split() — Divide an Array (分割数组)
Core idea: Split an array into multiple sub-arrays along an axis.
a = np.arange(12).reshape(4, 3)
# Split into 2 equal halves along rows (axis=0)parts = np.split(a, 2, axis=0) # [shape(2,3), shape(2,3)]
# Split at specific indicesnp.split(a, [1, 3], axis=0) # rows 0, rows 1–2, rows 3Note:
np.vsplit(a, n) and np.hsplit(a, n) are shorthand for axis=0 and axis=1 splits.7. Quick Comparison Table
| Function (函数) | In-place? | Returns |
|---|---|---|
reshape(shape) | No | View (same data) |
resize(shape) | Yes | None (modifies array) |
flatten() | No | Copy, 1-D |
ravel() | No | View, 1-D |
transpose() / .T | No | View |
concatenate() | No | New array |
split() | No | List of views |
💡 One-line Takeaway
Most shape-change operations return views (not copies) — changes propagate back to the original array, so use
Most shape-change operations return views (not copies) — changes propagate back to the original array, so use
.copy() when you need an independent result. NumPy Array Operations
https://lxy-alexander.github.io/blog/posts/numpy/api/02numpy-array-operations/