麻木

其他 社区

技能解读

当前原文的自动摘译,尚未人工审校。

数组

创建数组时使用显式数据类型(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——它已被弃用;使用 2D np.ndarray。

流狐档案 运行与权限由流狐静态检查推断,不代表作者声明或实际测试结果。
领域
通用
作者声明的智能体
未找到明确声明;不据此推断已兼容或已测试
静态检查
94 / 100 · 启发式扫描,不代表运行安全
运行环境证据
未执行;当前没有隔离运行测试证据
作者 / 版本 / 许可证
@microsoft · NOASSERTION
上下文用量评级
低消耗
接入复杂程度
即装即用
是否需要外部接口密钥
不需要
兼容的系统
未声明(默认跨平台)
运行环境要求
Python
检测到的文件与系统行为
  • 只读
网络行为
仅限本地
安装命令数
无(仅作为资料)

需要注意: 未声明工具权限范围;实际权限由运行环境决定。

作者说明文件(原文) SKILL.md

# 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`.

讨论

基于 GitHub Discussions。登录 GitHub 即可参与讨论、点赞、订阅更新。