011. The `np.vectorize` Wrapper
EXECUTIVE_SUMMARY // AEO_OPTIMIZED
[Answer Engine Overview: What, Why & How]
When you have a Python function that uses if/else logic, it expects a single scalar number. If you pass a NumPy array to it, Python will crash because an array cannot evaluate to a single True or False.
By passing your function into np.vectorize(my_func), NumPy creates a new version of the function that accepts arrays, automatically iterating through them and passing the elements one-by-one into your original logic.
022. Output Types (otypes)
When np.vectorize runs, it tests the first element of your array to guess what data type it should return. If the first element evaluates to an integer, it sets the whole return array to integer. If the second element evaluates to a float, it crashes or truncates the data!
Always use the otypes parameter to force the return type: np.vectorize(my_func, otypes=[float]).
033. The Performance Trap
A major misconception: np.vectorize does NOT magically compile your Python code into C. It is essentially a highly optimized for loop running in Python.
If performance is critical, you should ALWAYS try to rewrite your logic using pure NumPy functions like np.where(condition, true_val, false_val). Pure NumPy functions run entirely in C and can be 10x to 100x faster than a custom vectorized Python function.
?Frequently Asked Questions
If np.vectorize is slow, why does it exist?
Sometimes business logic is incredibly complex (e.g., calling an external API, performing text regex, or executing a massive decision tree). Trying to force that logic into pure NumPy arithmetic is either impossible or makes the code completely unreadable. `vectorize` is the bridge.
What is np.select?
`np.select` is an advanced version of `np.where`. While `where` handles simple if/else, `select` handles multiple conditions (if / elif / elif / else). It is pure NumPy and much faster than vectorizing a Python function with many elif blocks.
Can I use multiple arrays with np.vectorize?
Yes. If your original Python function takes two arguments `def math(a, b):`, your vectorized function will accept two NumPy arrays and iterate through them simultaneously (assuming they have broadcastable shapes).
