Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between frompyfunc and vectorize in numpy

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...

like image 324
Olivier Verdier Avatar asked Jul 20 '11 20:07

Olivier Verdier


People also ask

What does vectorize do in NumPy?

The concept of vectorized operations on NumPy allows the use of more optimal and pre-compiled functions and mathematical operations on NumPy array objects and data sequences. The Output and Operations will speed up when compared to simple non-vectorized operations. Example 1: Using vectorized sum method on NumPy array.

What does vectorize mean in python?

What is Vectorization ? Vectorization is used to speed up the Python code without using loop. Using such a function can help in minimizing the running time of code efficiently.

Does NumPy vectorize fast?

Again, some have observed vectorize to be faster than normal for loops, but even the NumPy documentation states: “The vectorize function is provided primarily for convenience, not for performance.

What is vectorized array?

"Vectorization" (simplified) is the process of rewriting a loop so that instead of processing a single element of an array N times, it processes (say) 4 elements of the array simultaneously N/4 times.


1 Answers

As JoshAdel points out, vectorize wraps frompyfunc. Vectorize adds extra features:

  • Copies the docstring from the original function
  • Allows you to exclude an argument from broadcasting rules.
  • Returns an array of the correct dtype instead of dtype=object

Edit: After some brief benchmarking, I find that vectorize is significantly slower (~50%) than frompyfunc for large arrays. If performance is critical in your application, benchmark your use-case first.

`

>>> a = numpy.indices((3,3)).sum(0)  >>> print a, a.dtype [[0 1 2]  [1 2 3]  [2 3 4]] int32  >>> def f(x,y):     """Returns 2 times x plus y"""     return 2*x+y  >>> f_vectorize = numpy.vectorize(f)  >>> f_frompyfunc = numpy.frompyfunc(f, 2, 1) >>> f_vectorize.__doc__ 'Returns 2 times x plus y'  >>> f_frompyfunc.__doc__ 'f (vectorized)(x1, x2[, out])\n\ndynamic ufunc based on a python function'  >>> f_vectorize(a,2) array([[ 2,  4,  6],        [ 4,  6,  8],        [ 6,  8, 10]])  >>> f_frompyfunc(a,2) array([[2, 4, 6],        [4, 6, 8],        [6, 8, 10]], dtype=object) 

`

like image 105
Stuart Berg Avatar answered Sep 25 '22 07:09

Stuart Berg