# NumPy

**NumPy** 是 Python 语言主要的开源数组编程库，提供多维数组对象，以及用于对数组进行数学、逻辑、线性代数、傅里叶变换和随机模拟运算的快速例程<sup>[1](https://numpy.org/doc/stable/index.html?highlight=mathematical+functions)</sup>。在 Python 世界中，NumPy 数组是数值数据的标准表示<sup>[2](https://arxiv.org/abs/1102.1523)</sup>，而该库是科学 Python 生态系统赖以构建的基础，并在横跨物理学、化学、天文学、生物学、工程学、金融学和经济学的研究流程中发挥作用<sup>[3](https://arxiv.org/abs/2006.10256)</sup>。

| 关键事实 | 详情 |
|---|---|
| 提供的功能 | 一个多维、类型一致、带步长的内存内数组对象（`ndarray`），以及用于对数组进行快速运算的例程<sup>[1](https://numpy.org/doc/stable/index.html?highlight=mathematical+functions)</sup><sup> • </sup><sup>[4](https://doi.org/10.1038/s41586-020-2649-2)</sup> |
| 以 NumPy 之名首次发布 | NumPy 0.9.2，2006 年 1 月，是 Numeric 和 Numarray 两者的后继，由特拉维斯·奥利芬特（Travis Oliphant）宣布<sup>[5](https://mail.python.org/pipermail/numpy-discussion/2006-January/005548.html)</sup> |
| 重大版本 | NumPy 2.0.0，2024 年 6 月 16 日，是 2006 年以来首个重大版本，由 212 名贡献者通过 1078 个拉取请求完成，包含一次 ABI 破坏<sup>[6](https://numpy.org/news/)</sup> |
| 核心机制 | 通用函数（ufunc）逐元素运算，支持广播和类型转换；循环在编译后的 C 中而非 Python 中进行<sup>[7](https://numpy.org/doc/stable/user/basics.ufuncs.html)</sup><sup> • </sup><sup>[8](https://numpy.org/doc/stable/user/basics.broadcasting)</sup> |
| 线性代数后端 | BLAS 和 LAPACK，并在可用时优先使用 OpenBLAS、MKL 和 ATLAS 等优化实现<sup>[9](https://numpy.org/doc/stable/reference/routines.linalg.html)</sup> |
| 硬件范围 | 仅 CPU；GPU 和分布式数组通过 DLPack 和 `__array_ufunc__` 等互操作性协议处理<sup>[10](https://numpy.org/neps/nep-0047-array-api-standard.html)</sup> |
| 治理 | 社区拥有的项目，隶属于 501(c)3 NumFOCUS 基金会，以 BSD 风格许可证发布<sup>[11](https://numpy.org/devdocs/dev/governance/governance.html)</sup> |
| 里程碑出版物 | 哈里斯（Harris）等，《用 NumPy 进行数组编程》（Array programming with NumPy），《自然》（*Nature*）585，357–362（2020）<sup>[4](https://doi.org/10.1038/s41586-020-2649-2)</sup> |

## 数组结构

NumPy 数组是一种由两部分组成的数据结构：存放实际元素的数据缓冲区，以及描述数据类型、形状和步长的元数据；步长是沿每个维度在相邻元素之间移动所用的字节偏移<sup>[12](https://numpy.org/doc/stable/user/basics.copies.html?highlight=indexing)</sup>。由于元数据与缓冲区分离，同一块内存可以用不同的形状、偏移、字节序或 dtype 重新解释，而无需移动任何字节<sup>[13](https://numpy.org/doc/1.21/reference/internals.html)</sup>。

**视图与副本。** 只改变元数据的操作产生视图，视图共享底层数据缓冲区，因此通过视图所做的修改会出现在原数组中。基本索引（例如切片）总是创建视图。同时复制缓冲区和元数据的操作产生副本，而使用索引数组的高级索引总是创建副本<sup>[12](https://numpy.org/doc/stable/user/basics.copies.html?highlight=indexing)</sup>。`base` 属性用于区分这两种情况：对视图返回原数组，对副本返回 `None`<sup>[12](https://numpy.org/doc/stable/user/basics.copies.html?highlight=indexing)</sup>。避免不必要的数据复制是三项技术之一；另外两项是向量化和尽量减少运算次数，它们共同使高层语言中的数值计算变得高效<sup>[2](https://arxiv.org/abs/1102.1523)</sup>。

## 通用函数与广播

通用函数（universal function），或称 ufunc，是一种对 `ndarray` 对象逐元素进行运算的函数，支持数组广播、类型转换和其他标准特性；它是一个向量化封装，封装的是接收固定数量输入并产生固定数量输出的函数<sup>[7](https://numpy.org/doc/stable/user/basics.ufuncs.html)</sup>。许多内置 ufunc 用编译后的 C 代码实现<sup>[7](https://numpy.org/doc/stable/user/basics.ufuncs.html)</sup>。广义通用函数（gufunc）作用于向量和矩阵等子数组，而非单个标量；`numpy.add` 是逐元素 ufunc，而 `numpy.matmul` 是 gufunc，后者实现了 NumPy 1.10.0 引入的 `@` 矩阵乘法运算符<sup>[7](https://numpy.org/doc/stable/user/basics.ufuncs.html)</sup><sup> • </sup><sup>[9](https://numpy.org/doc/stable/reference/routines.linalg.html)</sup>。

所有 ufunc 都有五个方法：四个类归约方法 `reduce`、`accumulate`、`reduceat` 和 `outer`，以及用于通过高级索引进行原地运算的 `at`<sup>[7](https://numpy.org/doc/stable/user/basics.ufuncs.html)</sup>。包括 ndarray 子类在内的类，可以通过定义 `__array_ufunc__` 等特殊方法来覆盖 ufunc 作用于它们时的行为<sup>[7](https://numpy.org/doc/stable/user/basics.ufuncs.html)</sup>。

**广播**描述 NumPy 在算术运算期间如何处理形状不同的数组：在约束条件下，较小的数组会向较大的数组广播，使两者的形状变得兼容。这使运算向量化，从而让循环在 C 中而非 Python 中进行，并且无需不必要地复制数据；不过也存在广播导致内存使用效率低的情况<sup>[8](https://numpy.org/doc/stable/user/basics.broadcasting)</sup>。根据广播规则，某一维度大小为 1 的输入会沿该维度复用其第一个数据条目，实现方式是将该维度的步长设为 0<sup>[7](https://numpy.org/doc/stable/user/basics.ufuncs.html)</sup>。

## 线性代数与编译后端

`numpy.linalg` 函数依赖 BLAS 和 LAPACK，为标准线性代数算法提供高效的底层实现。NumPy 自身可以提供部分参考实现的 C 版本，但在可能时，它会使用充分利用处理器专用功能的高度优化多线程库，例如 OpenBLAS、MKL 和 ATLAS；可能需要环境变量和 threadpoolctl 之类的包来控制线程数<sup>[9](https://numpy.org/doc/stable/reference/routines.linalg.html)</sup>。

SciPy 库有一个与之部分重叠的 `linalg` 子模块，提供 NumPy 缺少的函数，例如 LU 分解和 Schur 分解、额外的伪逆方法，以及矩阵对数之类的矩阵超越函数。某些 NumPy 函数具有更灵活的广播：`numpy.linalg.solve` 能处理堆叠数组，而 `scipy.linalg.solve` 只接受单个方阵作为其第一个参数<sup>[9](https://numpy.org/doc/stable/reference/routines.linalg.html)</sup>。文档将“矩阵”视为二维 `numpy.array`；`numpy.matrix` 对象不再被推荐，即使在线性代数中也是如此<sup>[9](https://numpy.org/doc/stable/reference/routines.linalg.html)</sup>。

## 历史

NumPy 源自两个更早的 Python 数组包 Numeric 和 Numarray，二者均已不再维护<sup>[14](https://numpy.org/history/)</sup>。20 世纪 90 年代中期，一个国际志愿者团队开始开发用于高效数组计算的数据结构，后来演变为 N 维 NumPy 数组<sup>[2](https://arxiv.org/abs/1102.1523)</sup>。NumPy 0.9.2 由特拉维斯·奥利芬特于 2006 年 1 月 5 日宣布，是以新名称发布的首个版本，由两个前身的代码构建而成，并引入了 Numarray 更快的排序函数，使排序速度达到原来的 2 至 3 倍<sup>[5](https://mail.python.org/pipermail/numpy-discussion/2006-January/005548.html)</sup>。

项目自己的历史页面记载，它最初资金很少，主要由研究生编写，其中许多人没有计算机科学教育背景<sup>[14](https://numpy.org/history/)</sup>。NumPy 1.22.0（2021 年 12 月 31 日）增加了 DLPack 后端、数组 API 标准的初步版本（NEP 47），以及主命名空间基本完整的类型注解，共有 153 名贡献者通过 609 个拉取请求完成<sup>[6](https://numpy.org/news/)</sup>。NumPy 2.0.0 随后于 2024 年 6 月 16 日发布，是 2006 年以来首个重大版本，包含无法在次要版本中出现的破坏性变更，包括一次 ABI 破坏、类型提升规则变更和 API 变更<sup>[6](https://numpy.org/news/)</sup>。NumPy 2.1.0 增加了对自由线程 CPython 3.13 的实验性支持，其中全局解释器锁被禁用<sup>[15](https://numpy.org/doc/stable/reference/thread_safety.html)</sup>，NumPy 2.5.0（2026 年 6 月 21 日）取消了对 Python 3.11 的支持，并增加了符合数组 API 标准的降序排序<sup>[6](https://numpy.org/news/)</sup>。

## 生态系统角色与互操作性

NumPy 如此普及，以至于若干面向专门受众的项目开发了自己的类 NumPy 接口和数组对象，而 NumPy 日益成为这些库之间的互操作层<sup>[3](https://arxiv.org/abs/2006.10256)</sup>。NumPy 本身仅支持 CPU，在 CPU 上提供内存中的多维、类型一致的数组<sup>[4](https://doi.org/10.1038/s41586-020-2649-2)</sup>。GPU 数组（CuPy）、稀疏数组、并行数组（Dask），以及 TensorFlow 和 PyTorch 中的类 NumPy 实现，都为各自的存储策略重新实现了 NumPy API<sup>[16](https://numpy.org/doc/1.26/user/basics.interoperability.html)</sup>。

若干协议使这一点在实践中可行。`__array_ufunc__` 和 `__array_function__` 协议让外部对象将 NumPy 运算交由自己的实现执行；例如，CuPy 数组实现了 `__array_ufunc__`，因此对 CuPy 数组调用 `np.exp` 会运行相应的 CUDA 或 ROCm 代码，并返回一个 CuPy 数组<sup>[17](https://numpy.org/doc/stable/user/basics.interoperability.html?highlight=cuda)</sup>。DLPack 为带步长的 n 维数组定义了一种内存布局，并且是唯一具备设备支持的主要交换协议，允许 PyTorch 和 CuPy 等库之间进行 GPU 到 GPU 的交换；NumPy 的 `np.from_dlpack` 将 CPU 数据导入为只读视图<sup>[10](https://numpy.org/neps/nep-0047-array-api-standard.html)</sup><sup> • </sup><sup>[17](https://numpy.org/doc/stable/user/basics.interoperability.html?highlight=cuda)</sup>。在 NumPy 2.0 中，项目根据 NEP 56 在主命名空间中采用了数组 API 标准（2022.12 版），为 CuPy、JAX 和 PyTorch 等库提供了一个更明确、更精简的 API 接口面作为目标<sup>[18](https://numpy.org/neps/roadmap.html)</sup>。

NumPy 的影响力延伸到著名的科学工作：为分析引力波而发布的脚本依赖导入 NumPy，而为 M87 黑洞成像的工作直接引用了它<sup>[14](https://numpy.org/history/)</sup>。截至 2024 年 5 月，PyPI 下载量每月超过 2 亿次<sup>[18](https://numpy.org/neps/roadmap.html)</sup>。

## 治理与社区

NumPy 项目是一个开源软件项目，隶属于 501(c)3 NumFOCUS 基金会；该基金会作为其财务赞助方，是唯一与该项目有正式关系的法律实体。该软件以 BSD 或类似许可证发布，并托管在 `numpy` 组织下的公共 GitHub 仓库中<sup>[11](https://numpy.org/devdocs/dev/governance/governance.html)</sup>。项目方向的决策由社区共识做出，指导委员会负责促进共识、管理资源，并在正常社区流程失效时做出决策<sup>[11](https://numpy.org/devdocs/dev/governance/governance.html)</sup>。开发优先级记录在公开路线图中，并通过 NumPy 增强提案（NEP）记录<sup>[19](https://numpy.org/neps/index.html)</sup>。

## 性能实践

三项技术支撑使用 NumPy 进行高效数值计算：向量化计算、避免在内存中复制数据，以及尽量减少运算次数<sup>[2](https://arxiv.org/abs/1102.1523)</sup>。许多 NumPy 运算会释放 Python 全局解释器锁（GIL），因此当每个工作线程拥有自己的数组时，多线程 Python 代码可以获得并行性能；在会修改数据的线程之间共享数组可能导致不一致、易发生竞争的结果，而对 `dtype=np.object_` 数组的运算不会释放 GIL<sup>[15](https://numpy.org/doc/stable/reference/thread_safety.html)</sup>。正在进行的性能工作集中在以下几个方面：通过抽象层在多种硬件平台上提供改进的通用 SIMD 内建函数（Universal SIMD intrinsics）、内部实现从 C 到 C++ 的过渡，以及采用 Google Highway 作为 SIMD 基础设施<sup>[18](https://numpy.org/neps/roadmap.html)</sup>。

## References

1. [NumPy documentation, NumPy v2.3 Manual](https://numpy.org/doc/stable/index.html?highlight=mathematical+functions)
2. [Stéfan van der Walt, S. Chris Colbert, Gaël Varoquaux (2011). The NumPy array: a structure for efficient numerical computation. arXiv:1102.1523](https://arxiv.org/abs/1102.1523)
3. [Charles R. Harris et al. (2020). Array Programming with NumPy. arXiv:2006.10256](https://arxiv.org/abs/2006.10256)
4. [Harris, C.R., Millman, K.J., van der Walt, S.J. et al. Array programming with NumPy. Nature 585, 357–362 (2020)](https://doi.org/10.1038/s41586-020-2649-2)
5. [Travis Oliphant (5 January 2006). ANN: Release of NumPy 0.9.2, numpy-discussion mailing list](https://mail.python.org/pipermail/numpy-discussion/2006-January/005548.html)
6. [NumPy News, numpy.org](https://numpy.org/news/)
7. [Universal functions (ufunc) basics, NumPy v2.5 Manual](https://numpy.org/doc/stable/user/basics.ufuncs.html)
8. [Broadcasting, NumPy v2.5 Manual](https://numpy.org/doc/stable/user/basics.broadcasting)
9. [Linear algebra, NumPy v2.5 Manual](https://numpy.org/doc/stable/reference/routines.linalg.html)
10. [NEP 47 — Adopting the array API standard](https://numpy.org/neps/nep-0047-array-api-standard.html)
11. [NumPy project governance and decision-making, NumPy devdocs](https://numpy.org/devdocs/dev/governance/governance.html)
12. [Copies and views, NumPy v2.5 Manual](https://numpy.org/doc/stable/user/basics.copies.html?highlight=indexing)
13. [NumPy internals, NumPy v1.21 Manual](https://numpy.org/doc/1.21/reference/internals.html)
14. [History of NumPy, numpy.org](https://numpy.org/history/)
15. [Thread Safety, NumPy v2.5 Manual](https://numpy.org/doc/stable/reference/thread_safety.html)
16. [Interoperability with NumPy, NumPy v1.26 Manual](https://numpy.org/doc/1.26/user/basics.interoperability.html)
17. [Interoperability with NumPy, NumPy v2.5 Manual](https://numpy.org/doc/stable/user/basics.interoperability.html?highlight=cuda)
18. [NumPy roadmap, NumPy Enhancement Proposals](https://numpy.org/neps/roadmap.html)
19. [Roadmap & NumPy enhancement proposals, numpy.org/neps](https://numpy.org/neps/index.html)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 30, 2026; Oct 15, 2026 · Edited: Sep 30, 2026; Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 物质世界与数学 › 数学与统计学 › 数与代数 › Linear and multilinear algebra › Numerical linear algebra › Numerical linear algebra software

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/numpy

English original: https://www.edgepedia.org/numpy
