NumPy
用于 Python 科学计算的基础包
NumPy 2.3.0 发布!
2025-06-07
强大的 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 中,Python 是一种更容易学习和使用的语言。这种能力带来了简洁性:NumPy 中的解决方案通常清晰而优雅。

NumPy 的 API 是编写库的起点,用于利用创新硬件、创建专用数组类型或添加 NumPy 提供之外的功能。

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

NumPy 处于丰富的数据科学库生态系统的核心。典型的数据科学探索性工作流程可能如下所示:

对于高数据量,DaskRay 旨在进行扩展。稳定的部署依赖于数据版本控制 (DVC)、实验跟踪 (MLFlow) 和工作流自动化 (AirflowDagsterPrefect)。

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 是另一个深度学习库,在计算机视觉和自然语言处理研究人员中很受欢迎。

诸如装箱(binning)、套袋(bagging)、堆叠(stacking)和提升(boosting)等被称为集成方法的统计技术,是 XGBoostLightGBMCatBoost(最快的推理引擎之一)等工具实现的机器学习算法。 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 所能处理的更大的数据集。

案例研究