Skip to content

numpy/np-vectorize

Rule np-vectorize
Pack NumPy
Severity warning
Fix none, edit by hand
Library versions every version

np.vectorize, np.frompyfunc and np.apply_along_axis still call a Python function once per value or slice. Where the function is arithmetic or a condition, write it on whole arrays.

@np.vectorize
def clipped(x):
return min(x, 3.0)
return np.minimum(x, 3.0)

The rule reports np.vectorize, np.frompyfunc and np.apply_along_axis, whether called or used as a decorator.

xpd has no automatic fix, so make the change by hand.

xpd prints this message with the issue. A name that starts with $ stands for code from your file.

$F still calls the Python function once per value or once per slice, so it runs at the speed of a Python loop. Where the function is arithmetic or a condition, write it on whole arrays, for example with np.where or np.select.

Browse the other rules of the NumPy pack.