Loading...
Loading...
Compare original and translation side by side
import numpy as npfrom numpy import *import numpy as npfrom numpy import *| Use case | Function |
|---|---|
| Known values | |
| Zeros | |
| Ones | |
| Uninitialized (fill later) | |
| Integer range | |
| Evenly spaced floats | |
| Identity matrix | |
| Like existing array | |
| 使用场景 | 函数 |
|---|---|
| 已知值 | |
| 全零数组 | |
| 全一数组 | |
| 未初始化(后续填充) | |
| 整数序列 | |
| 等间距浮点数 | |
| 单位矩阵 | |
| 匹配现有数组形状 | |
dtypefloat64int64undefinedfloat64int64dtypeundefined
Do not rely on implicit upcasting — declare the dtype the data actually needs.
不要依赖隐式向上转换——声明数据实际需要的dtype。np.random.default_rng()np.random.default_rng()np.random.*np.random.randundefinednp.random.*np.random.randundefined
Pass `seed` to `default_rng` for reproducibility in tests and experiments.
在测试和实验中,将`seed`传入`default_rng`以保证结果可复现。undefinedundefined
Use `np.vectorize` only as a convenience wrapper for scalar functions — it does **not** improve performance since it still calls Python per element.
仅将`np.vectorize`用作标量函数的便捷包装器——它不会提升性能,因为仍会逐元素调用Python。tilerepeatValueErrorundefinedtilerepeatValueErrorundefined
Avoid broadcasting that produces very large intermediate arrays — use an explicit loop for memory-constrained cases.
避免广播产生过大的中间数组——在内存受限的情况下使用显式循环。x = np.arange(10)
y = x[2:5] # view — shares memory
y[0] = 99 # also changes x[2]x = np.arange(10)
idx = [1, 3, 5]
y = x[idx] # copy — independent of xarr.basey.base is None # True → copy
y.base is x # True → view of xx = np.arange(10)
y = x[2:5] # 视图——共享内存
y[0] = 99 # 同时修改x[2]x = np.arange(10)
idx = [1, 3, 5]
y = x[idx] # 副本——与原数组独立arr.basey.base is None # True → 副本
y.base is x # True → x的视图.copy()backup = original.copy().ravel().flatten()reshape(-1).copy()backup = original.copy().ravel().flatten()reshape(-1)arr = np.array([1, -2, 3, -4, 5])
positive = arr[arr > 0] # copy: [1, 3, 5]
arr[arr < 0] = 0 # in-place modification via boolean maskarr = np.array([1, -2, 3, -4, 5])
positive = arr[arr > 0] # 副本:[1, 3, 5]
arr[arr < 0] = 0 # 通过布尔掩码原地修改np.wherenp.whereundefinedundefinedundefinedundefinedmatrix = np.arange(12).reshape(3, 4)
row_sums = matrix.sum(axis=1) # sum each row → shape (3,)
col_max = matrix.max(axis=0) # max each column → shape (4,)matrix = np.arange(12).reshape(3, 4)
row_sums = matrix.sum(axis=1) # 对每行求和 → 形状(3,)
col_max = matrix.max(axis=0) # 对每列取最大值 → 形状(4,)| Scenario | Recommended dtype |
|---|---|
| ML model weights | |
| High-precision scientific | |
| Small integer counts (<32 k) | |
| Large integer counts | |
| Boolean flags | |
| Complex numbers | |
arr.astype(np.float32, copy=False)copy=False| 场景 | 推荐dtype |
|---|---|
| 机器学习模型权重 | |
| 高精度科学计算 | |
| 小整数计数(<32k) | |
| 大整数计数 | |
| 布尔标记 | |
| 复数 | |
arr.astype(np.float32, copy=False)copy=Falsex = np.array([200], dtype=np.int8) # max 127
x + 100 # array([ 44], dtype=int8) — silent overflow!x = np.array([200], dtype=np.int8) # 最大值为127
x + 100 # array([ 44], dtype=int8) —— 静默溢出!| Format | Function | Use case |
|---|---|---|
| Single array (binary) | | Fast, preserves dtype and shape |
| Multiple arrays | | Archive multiple arrays |
| Text (CSV etc.) | | Human-readable interchange |
undefined| 格式 | 函数 | 使用场景 |
|---|---|---|
| 单个数组(二进制) | | 速度快,保留dtype和形状 |
| 多个数组 | | 归档多个数组 |
| 文本(CSV等) | | 人类可读的交互格式 |
undefined
Prefer `.npy`/`.npz` over text formats for large arrays — binary I/O is faster and lossless.
对于大型数组,优先使用`.npy`/`.npz`而非文本格式——二进制I/O速度更快且无精度损失。-1flat = arr.reshape(-1) # flatten to 1-D (view when possible)
col = arr.reshape(-1, 1) # column vector
row = arr.reshape(1, -1) # row vectornp.newaxisNonea = np.array([1, 2, 3]) # shape (3,)
a_col = a[:, np.newaxis] # shape (3, 1)
a_row = a[np.newaxis, :] # shape (1, 3)-1flat = arr.reshape(-1) # 扁平化为1维数组(尽可能返回视图)
col = arr.reshape(-1, 1) # 列向量
row = arr.reshape(1, -1) # 行向量np.newaxisNonea = np.array([1, 2, 3]) # 形状(3,)
a_col = a[:, np.newaxis] # 形状(3, 1)
a_row = a[np.newaxis, :] # 形状(1, 3)np.linalgA = np.array([[1, 2], [3, 4]], dtype=np.float64)np.linalgA = np.array([[1, 2], [3, 4]], dtype=np.float64)
Never use `np.matrix` — it is deprecated. Use 2-D `ndarray` with `@` instead.
切勿使用`np.matrix`——它已被弃用。改用2维`ndarray`并配合`@`运算符。| Task | Idiomatic code |
|---|---|
| Import | |
| Array from list | |
| Shape / ndim / size | |
| Reshape | |
| Flatten (view) | |
| Flatten (copy) | |
| Transpose | |
| Boolean mask | |
| Axis aggregation | |
| Matrix multiply | |
| Copy | |
| Check view | |
| Cast dtype | |
| Random (modern) | |
| Save / load | |
| 任务 | 惯用代码 |
|---|---|
| 导入 | |
| 从列表创建数组 | |
| 形状/维度/元素数 | |
| 重塑 | |
| 扁平化(视图) | |
| 扁平化(副本) | |
| 转置 | |
| 布尔掩码 | |
| 轴聚合 | |
| 矩阵乘法 | |
| 复制 | |
| 检查是否为视图 | |
| 转换dtype | |
| 随机数生成(现代) | |
| 保存/加载 | |
references/performance-and-memory.mdreferences/array-operations.mdreferences/performance-and-memory.mdreferences/array-operations.md