NumPy
Python 科学计算的基础软件包
NumPy 2.5.0 已发布!
2026-06-21
强大的 N 维数组
NumPy 的向量化、索引和广播概念快速且通用,是当今数组计算的事实标准。
数值计算工具
NumPy 提供全面的数学函数、随机数生成器、线性代数例程、傅里叶变换等功能。
开源
NumPy 在宽松的 BSD 许可证下分发,并由一个充满活力、响应迅速且多元化的社区GitHub 上公开开发和维护。
可互操作
NumPy 支持广泛的硬件和计算平台,并能与分布式、GPU 和稀疏数组库良好协作。
高性能
NumPy 的核心是经过高度优化的 C 代码。在享受 Python 灵活性的同时,拥有编译代码的速度。
易于使用
NumPy 的高级语法使得具有不同背景或经验水平的程序员都能轻松使用并提高生产力。
尝试 NumPy

使用交互式 shell 在浏览器中尝试 NumPy

"""
To try the examples in the browser:
1. Type code in the input cell and press
   Shift + Enter to execute
2. Or copy paste the code, and click on
   the "Run" button in the toolbar
"""

# The standard way to import NumPy:
import numpy as np

# Create a 2-D array, set every second element in
# some rows and find max per row:

x = np.arange(15, dtype=np.int64).reshape(3, 5)
x[1:, ::2] = -99
x
# array([[  0,   1,   2,   3,   4],
#        [-99,   6, -99,   8, -99],
#        [-99,  11, -99,  13, -99]])

x.max(axis=1)
# array([ 4,  8, 13])

# Generate normally distributed random numbers:
rng = np.random.default_rng()
samples = rng.normal(size=2500)
samples

生态系统

几乎每一位使用 Python 的科学家都在借助 NumPy 的强大功能。

NumPy 将 C 和 Fortran 等语言的计算能力带到了 Python——一种更易于学习和使用的语言。这种强大的能力同时也带来了简洁:NumPy 的解决方案往往清晰且优雅。

当开发者编写库以利用创新硬件、创建专用数组类型或添加 NumPy 未提供的功能时,NumPy 的 API 是起点。

数组库功能与应用领域
DaskDask用于分析的分布式数组和高级并行处理,实现规模化性能。
CuPyCuPy用于 Python GPU 加速计算的 NumPy 兼容数组库。
JAXJAXNumPy 程序的组合转换:微分、向量化、即时编译到 GPU/TPU。
xarrayXarray用于高级分析和可视化的带标签、带索引的多维数组。
sparseSparse与 Dask 和 SciPy 稀疏线性代数集成的 NumPy 兼容稀疏数组库。
PyTorchPyTorch深度学习框架,加速从研究原型到生产部署的路径。
TensorFlowTensorFlow一个端到端的机器学习平台,可轻松构建和部署机器学习驱动的应用。
arrowArrow一个用于内存中列式数据和分析的跨语言开发平台。
xtensorxtensor具有广播和惰性计算功能的多维数组,用于数值分析。
awkwardAwkward Array使用类似 NumPy 的语法操作 JSON 类数据。
uarrayuarray将 API 与实现解耦的 Python 后端系统;unumpy 提供 NumPy API。
tensorlytensorly张量学习、代数和后端,可无缝使用 NumPy、PyTorch、TensorFlow 或 CuPy。
blosc2Blosc2针对内存中、磁盘上或远程压缩数组的加速计算。
Diagram of Python Libraries. The five catagories are 'Extract, Transform, Load', 'Data Exploration', 'Data Modeling', 'Data Evaluation' and 'Data Presentation'.

NumPy 是丰富的数据科学库生态系统的核心。一个典型的数据科学探索工作流程可能如下:

对于高数据量,DaskRay 旨在实现横向扩展。稳定的部署依赖于数据版本控制 (DVC)、实验跟踪 (MLFlow) 以及工作流自动化 (Airflow, DagsterPrefect)。

Diagram of three overlapping circles. The circles are labeled 'Mathematics', 'Computer Science' and 'Domain Expertise'. In the middle of the diagram, which has the three circles overlapping it, is an area labeled 'Data Science'.

NumPy 是 scikit-learnSciPy 等强大机器学习库的基础。随着机器学习的发展,建立在 NumPy 之上的库列表也在不断增加。TensorFlow 的深度学习功能具有广泛的应用——包括语音和图像识别、基于文本的应用、时间序列分析和视频检测。PyTorch 是另一个深度学习库,在计算机视觉和自然语言处理领域的研究人员中很受欢迎。

被称为集成方法 (Ensemble methods) 的统计技术,如装箱、装袋、堆叠和提升,属于 XGBoostLightGBMCatBoost(最快的推理引擎之一)等工具实现的 ML 算法。YellowbrickEli5 提供机器学习可视化功能。

A streamplot made in matplotlib
A scatter-plot graph made in ggpy
A box-plot made in plotly
A streamgraph made in altair
A pairplot of two types of graph, a plot-graph and a frequency graph made in seaborn"
A 3D volume rendering made in PyVista.
A multi-dimensionan image made in napari.
A Voronoi diagram made in vispy.

NumPy 是蓬勃发展的 Python 可视化领域的重要组成部分,其中包括 MatplotlibSeabornPlotlyAltairBokehHolovizVispyNapariPyVista 等。

NumPy 对大型数组的加速处理使研究人员能够可视化原生 Python 无法处理的超大数据集。

案例研究