Use
[func(i) for i in arr]
np.vectorize, even where it works is not faster.
Or
np.array(arr)+np.array([[1,2]])
for a true numpy 'vectorized' calculation, adding a (1,2) array to the (2,2) arr.
p.s.
arr is a list of tuples. If you can construct an array of tuples, I'll revisit the problem.
edit
We should have objected because you did not supply the error message! Especially since you are trying to understand vectorize.
In [307]: np.array(arr)
Out[307]:
array([[ 1, 3],
[ 9, 12]])
In [308]: vfunc = np.vectorize(func)
...: vfunc(arr)
Traceback (most recent call last):
File "<ipython-input-308-61dee032d8f1>", line 2, in <module>
vfunc(arr)
File "/usr/local/lib/python3.8/dist-packages/numpy/lib/function_base.py", line 2163, in __call__
return self._vectorize_call(func=func, args=vargs)
File "/usr/local/lib/python3.8/dist-packages/numpy/lib/function_base.py", line 2241, in _vectorize_call
ufunc, otypes = self._get_ufunc_and_otypes(func=func, args=args)
File "/usr/local/lib/python3.8/dist-packages/numpy/lib/function_base.py", line 2201, in _get_ufunc_and_otypes
outputs = func(*inputs)
File "<ipython-input-306-ab62cb78d2b7>", line 4, in func
return (i[0]+1, i[1]+2)
IndexError: invalid index to scalar variable.
The error occurs in your function because you are trying to index a scalar variable. i is a number, not a tuple.
Testing my initial suggestions:
In [309]: [func(i) for i in arr]
Out[309]: [(2, 5), (10, 14)]
In [310]: np.array(arr)+np.array([1,2])
Out[310]:
array([[ 2, 5],
[10, 14]])
Or making a 2 element array:
In [311]: A = np.empty(2, object)
In [312]: A[:]=arr
In [313]: A
Out[313]: array([(1, 3), (9, 12)], dtype=object)
In [314]: A[0]
Out[314]: (1, 3)
In [315]: vfunc(A)
Out[315]: (array([ 2, 10]), array([ 5, 14]))
Compare this A with Out[307]. Very different arrays.
Another way to diagnose this is to add a print to the func.
In [317]: def func(i):
...: print(i)
...: return (i[0]+1, i[1]+2)
...:
In [318]: vfunc = np.vectorize(func)
In [319]: vfunc(arr)
1
Traceback (most recent call last):
File "<ipython-input-319-62576075c0b8>", line 1, in <module>
...
IndexError: invalid index to scalar variable.
vectorized pass the first element of np.array(arr), not the first tuple of arr, the scalar 1.
Applied to the object array:
In [320]: vfunc(A)
(1, 3) # the documented trial call
(1, 3)
(9, 12)
Out[320]: (array([ 2, 10]), array([ 5, 14]))
Now we see it is passing tuples to the function.