NumPy
Python 進行科學運算的基礎套件
NumPy 2.4.0 已發佈!
2025-12-20
強大的 N 維陣列
NumPy 的向量化、索引與廣播(broadcasting)概念快速且通用,是現今陣列運算的實質標準。
數值運算工具
NumPy 提供全面的數學函數、亂數產生器、線性代數運算、傅立葉轉換等功能。
開源軟體
NumPy 以自由的 BSD 授權條款發佈,由活躍、積極且多元的社群GitHub 上公開開發與維護。
高度互通性
NumPy 支援廣泛的硬體與運算平台,並能與分散式運算、GPU 及稀疏矩陣(sparse array)函式庫良好整合。
高效能
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 本身提供的功能時,NumPy 的 API 往往是首選起點。

陣列函式庫功能與應用領域
DaskDask提供用於分析的分散式陣列與進階平行運算,實現大規模效能。
CuPyCuPy相容於 NumPy 的陣列函式庫,支援 Python 的 GPU 加速運算。
JAXJAX可組合的 NumPy 程式轉換:支援微分、向量化,並可即時編譯至 GPU/TPU。
xarrayXarray為進階分析與視覺化提供標註化、索引化的多維陣列。
sparseSparse相容於 NumPy 的稀疏陣列函式庫,可與 Dask 及 SciPy 的稀疏線性代數整合。
PyTorchPyTorch深度學習框架,加速從研究原型開發到生產環境部署的過程。
TensorFlowTensorFlow端到端的機器學習平台,可輕鬆建構與部署機器學習應用程式。
arrowArrow跨語言開發平台,用於記憶體內的列式資料(columnar data)處理與分析。
xtensorxtensor支援廣播與惰性運算(lazy computing)的多維陣列,用於數值分析。
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)的統計技術,如分箱(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 視覺化生態系統中的重要組件,其中包括 MatplotlibSeabornPlotlyAltairBokehHolovizVispyNapari 以及 PyVista 等。

NumPy 對大型陣列的加速處理能力,使研究人員能夠視覺化遠超原生 Python 所能處理的龐大資料集。

案例研究