Most efficient way to map function over numpy array
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
The most efficient way to apply a function over a NumPy array is vectorization — using NumPy's built-in operations that execute in compiled C code. For element-wise math, use NumPy ufuncs directly (np.sqrt(arr), arr * 2). For custom functions, try to rewrite them using NumPy operations. As a last resort, use np.vectorize() or np.frompyfunc(), which are convenience wrappers around Python loops and offer no real speedup. Avoid Python for loops over NumPy arrays — they are 10-100x slower than vectorized operations.
Vectorized Operations (Fastest)
NumPy ufuncs are implemented in C and operate on entire arrays without Python loop overhead. Always prefer these when possible.
np.where for Conditional Logic
np.where is the vectorized equivalent of if-else and runs at C speed.
Rewriting Custom Functions as Vectorized
np.vectorize (Convenience, Not Speed)
np.vectorize does NOT make the function run faster — it is essentially a for loop with broadcasting support. It is only a convenience for making scalar functions accept arrays.
Performance Comparison
Vectorized NumPy is 100-200x faster than Python loops for large arrays.
Using np.frompyfunc
np.frompyfunc creates a ufunc that returns an object array. It is marginally faster than np.vectorize but still a Python-level loop.
Pandas apply vs NumPy
If your data is in a pandas DataFrame, still prefer NumPy operations on the underlying .values array for best performance.
Lookup Table for Discrete Mapping
Common Pitfalls
- Using
np.vectorizeexpecting C-speed:np.vectorizeis a Python loop with a nice API. It does not compile or optimize the function. For real speedups, rewrite using NumPy operations or use Numba (@numba.vectorize). - Python
forloop over large arrays: Iterating over a million-element array in Python takes hundreds of milliseconds. The same operation vectorized takes single-digit milliseconds. Always try vectorization first. - Applying pandas
applyunnecessarily:df['col'].apply(lambda x: x * 2)is much slower thandf['col'] * 2. Use vectorized pandas/NumPy operations before reaching forapply. - Creating intermediate arrays:
np.where(arr > 0, arr, 0)creates a boolean mask array internally. For very large arrays, this doubles memory usage. Use in-place operations (np.clip(arr, 0, None, out=arr)) when memory is constrained. - Ignoring Numba for complex custom functions: When a function cannot be expressed in NumPy operations,
@numba.jitcompiles Python to machine code and achieves near-C performance:@numba.vectorize def f(x): return x**2 + 1.
Summary
- Use NumPy built-in operations (
np.sqrt,np.where, arithmetic) for maximum speed - Rewrite custom functions using NumPy vectorized operations instead of if/else
np.vectorizeis for convenience, not performance — it wraps a Python loop- Avoid Python
forloops andlist(map())on NumPy arrays — they are 100x slower - Use lookup table indexing (
lookup[arr]) for mapping discrete integer values - For functions that cannot be vectorized, use Numba
@jitfor compiled performance
Related reading
- Most efficient way to reverse a numpy array
- Moving matplotlib legend outside of the axis makes it cutoff by the figure box
- Moving x-axis to the top of a plot in matplotlib
- Multi-class classification in libsvm
- Move all odd positioned element to left half and even positioned to right half in-place
- Move duplicates to the end of a sorted array
- Most idiomatic way to convert None to empty string?
- Most pythonic way to delete a file which may not exist

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.