NumExpr is a fast numerical expression evaluator for NumPy. It speeds up mathematical computations by using multithreading and efficient evaluation techniques, making it ideal for large datasets or complex numerical expressions.
Author
Benedict Thekkel
1. What is NumExpr?
NumExpr is a Python library for evaluating numerical expressions faster than pure Python or NumPy.
It compiles expressions into bytecode and evaluates them efficiently using vectorized operations and multi-threading.
2. Key Features
Fast Execution:
Evaluates expressions more quickly than NumPy for large arrays by avoiding intermediate arrays.
Multithreading:
Uses multiple CPU cores for faster computations.
Memory Efficient:
Minimizes memory usage by not creating temporary arrays for intermediate results.
NumPy Integration:
Works seamlessly with NumPy arrays.
3. Installation
To install NumExpr, use pip:
pip install numexpr
4. Basic Usage
To evaluate a mathematical expression:
import numexpr as ne# Define a NumPy arrayimport numpy as npa = np.arange(1e6)# Evaluate an expressionresult = ne.evaluate("a ** 2 + 3 * a - 5")
ne.evaluate() takes a string expression and evaluates it.
It supports standard mathematical operations (+, -, *, /, **) and functions (e.g., sin, cos, log).
import numexpr as ne# Define a NumPy arrayimport numpy as npa = np.arange(1e6)# Evaluate an expressionresult = ne.evaluate("a ** 2 + 3 * a - 5")a, result