NumPy
NumPy is the core Python library for numerical computing, providing a fast, memory-efficient N-dimensional array type and vectorized math operations that most of the Python data-science stack is built on.
Overview
NumPy (short for Numerical Python) is the foundational package for numerical computing in Python. Its central object, the ndarray, is a fixed-type, contiguous, N-dimensional array that supports fast element-wise arithmetic, linear algebra, random number generation, and broadcasting. Because it stores data as compact typed buffers rather than lists of Python objects, NumPy operations run orders of magnitude faster than equivalent pure-Python loops. It has been the substrate that pandas, scikit-learn, PyTorch, and much of the scientific Python ecosystem are built on top of.
The ndarray and vectorization
An ndarray has a fixed dtype (e.g. float64, int32) and shape. Operations are vectorized: instead of looping over elements in Python, you apply a function to the whole array at once, and NumPy executes the loop in compiled C code.
Copy code
import numpy as np
a = np.array([1, 2, 3, 4])
b = np.array([10, 20, 30, 40])
c = a * b + 1 # vectorized, no Python-level loop
mean = c.mean()
Broadcasting lets NumPy apply operations between arrays of different but compatible shapes (e.g. a matrix and a row vector) without manually replicating data.
NumPy and DuckDB
DuckDB's Python client interoperates with NumPy in both directions. DuckDB can query NumPy arrays directly by name via a replacement scan, and query results can be exported as NumPy arrays with .fetchnumpy().
Copy code
import duckdb
import numpy as np
my_arr = np.array([(1, 9.0), (2, 8.0), (3, 7.0)])
duckdb.sql("SELECT * FROM my_arr").show()
result = duckdb.sql("SELECT unnest([1, 2, 3]) AS x").fetchnumpy()
This makes it easy to push heavy filtering, joining, and aggregation into DuckDB's vectorized SQL engine and only pull the smaller, already-reduced result into NumPy for downstream numerical work like modeling or plotting.
Related terms
scikit-learn is a widely used Python library for classical machine learning, providing a consistent API for classification, regression, clustering, and preprocessing built on NumPy and SciPy.
Python →Python is a high-level, interpreted programming language known for its simplicity and readability.
LIST and ARRAY type →LIST and ARRAY are nested SQL data types for storing an ordered collection of values in a single column — LIST is variable-length, ARRAY is fixed-length.
pandas →pandas is a powerful, open-source data manipulation and analysis library for Python.
Vectorized execution →Vectorized execution is a query processing model where each operator processes a batch (a "vector") of values at once, instead of one row at a time, to reduce interpretation overhead and better use modern CPUs.
PyPi →PyPI, short for Python Package Index, is the official repository for third-party Python software packages.
FAQS
No. NumPy provides the low-level N-dimensional array and numerical routines; pandas builds labeled, tabular DataFrame and Series structures on top of NumPy (and increasingly Arrow) for working with heterogeneous, labeled data.
Yes. DuckDB's Python client can find NumPy arrays in the local scope by variable name and query them directly with SQL, no explicit registration required.
