What is the difference between vectorize and frompyfunc in numpy?
Both seem very similar. What is a typical use case for each of them?
Edit: As JoshAdel indicates, the class vectorize
seems to be built upon frompyfunc
. (see the source). It is still unclear to me whether frompyfunc
may have any use case that is not covered by vectorize
...
Although both methods provide you a way to build your own ufunc, numpy.frompyfunc method always returns a python object, while you could specify a return type when using numpy.vectorize method
I'm not sure what the different use cases for each is, but if you look at the source code (/numpy/lib/function_base.py), you'll see that
vectorize
wrapsfrompyfunc
. My reading of the code is mostly thatvectorize
is doing proper handling of the input arguments. There might be particular instances where you would prefer one vs the other, but it would seem thatfrompyfunc
is just a lower level instance ofvectorize
.As JoshAdel points out,
vectorize
wrapsfrompyfunc
. Vectorize adds extra features:Edit: After some brief benchmarking, I find that
vectorize
is significantly slower (~50%) thanfrompyfunc
for large arrays. If performance is critical in your application, benchmark your use-case first.`
`