麻木
- 作者仓库星标 2,412
- 许可证 NOASSERTION
- 作者仓库 debugpy
- 领域
- 通用
- 作者声明的智能体
- 未找到明确声明;不据此推断已兼容或已测试
- 静态检查
- 94 / 100 · 启发式扫描,不代表运行安全
- 运行环境证据
- 未执行;当前没有隔离运行测试证据
- 作者 / 版本 / 许可证
- @microsoft · NOASSERTION
- 上下文用量评级
- 低消耗
- 接入复杂程度
- 即装即用
- 是否需要外部接口密钥
- 不需要
- 兼容的系统
- 未声明(默认跨平台)
- 运行环境要求
- Python
- 检测到的文件与系统行为
-
- 只读
- 网络行为
- 仅限本地
- 安装命令数
- 无(仅作为资料)
需要注意: 未声明工具权限范围;实际权限由运行环境决定。
# Skill: NumPy
Best practices for numerical computing with NumPy including arrays, broadcasting, and vectorization.
## When to Use
Apply this skill when doing numerical computing with NumPy — arrays, broadcasting, linear algebra, random sampling.
## Arrays
- Use explicit dtypes (`np.float64`, `np.int32`) when creating arrays.
- Prefer `np.zeros`, `np.ones`, `np.empty`, `np.arange`, `np.linspace` over list-based construction.
- Use structured arrays or separate arrays instead of object arrays.
## Vectorization
- Replace Python loops with vectorized NumPy operations wherever possible.
- Use broadcasting rules to operate on arrays of different shapes without explicit expansion.
- Use `np.where()` for conditional element-wise operations.
## Memory
- Use `np.float32` instead of `np.float64` when precision is not critical to halve memory.
- Use views (`reshape`, slicing) instead of copies when data doesn't need mutation.
- Use `np.memmap` for arrays too large to fit in RAM.
## Random
- Use `np.random.default_rng(seed)` (new Generator API) instead of `np.random.seed()`.
- Always seed random generators in tests for reproducibility.
## Pitfalls
- Don't compare floats with `==`; use `np.allclose()` or `np.isclose()`.
- Beware of silent integer overflow in integer arrays.
- Avoid `np.matrix` — it's deprecated; use 2D `np.ndarray`.
章节与文件索引
按说明文件中的顺序排列。
数组
矢量化
内存
随机
陷阱
文件/命令 -> np.float64 · np.int32 · np.zeros · np.ones · np.empty · np.arange · np.linspace · np.where()
内容校验值 -> 740c7c1c0c8b
当前原文的自动摘译,尚未人工审校。
数组
创建数组时使用显式数据类型(
np.float64、np.int32)。与基于列表的构造相比,更喜欢np.zeros、np.ones、np.empty、np.arange、np.linspace。使用结构化数组或单独的数组而不是对象数组。矢量化
尽可能用向量化 NumPy 运算替换 Python 循环。使用广播规则对不同形状的数组进行操作,无需显式扩展。使用
np.where()进行条件逐元素运算。内存
当精度对于减半内存并不重要时,使用
np.float32代替np.float64。当数据不需要突变时,使用视图(reshape,切片)而不是副本。对于太大而无法放入 RAM 的数组,请使用np.memmap。随机
使用
np.random.default_rng(seed)(新的生成器API)而不是np.random.seed()。始终在测试中播种随机生成器以确保可重复性。陷阱
不要将浮点数与
==进行比较;使用np.allclose()或np.isclose()。谨防整数数组中的静默整数溢出。避免使用np.matrix——它已被弃用;使用 2Dnp.ndarray。