0

I have a Python function, f, which takes a pair of numbers and return a calculation result on them, say, x+y

def f(x,y):
          return x+y

How can I vectorize f, so for given vectors X=(x1,...,xn) and Y=(y1,...,yn)

f_vectorized(X,Y) returns the array of f(x1,y1), f(x2,y2)...?

EDIT

Above, x+y is an example, but the actual calculation is more complex, so by regarding x and y as numpy vector does not necessarily work out-of-box.

1
  • Without seeing actual code, we can't tell you how to vectorize this thing properly. Using list comprehensions or np.vectorize will sacrifice all the performance benefits of NumPy, which is likely to be around a 1000x slowdown. Unless your code involves loops that fundamentally need to execute for a number of times that depends on the input, something like this should be fairly straightforward to vectorize. Commented Jan 11, 2016 at 21:36

2 Answers 2

5

What about using numpy as input?

import numpy as np

def f(x,y):
     return x+y

a = np.array([0,1,2,3])
b = np.array([1,2,3,4])

In [430]: f(a,b)
Out[430]: array([1, 3, 5, 7])

EDIT

For more complex function you could use list comprehension and zip:

In [451]: [f(*par) for par in  zip(a, b)]
Out[451]: [1, 3, 5, 7]

EDIT2

Or you could use np.vectorize as you mentioned in the comment:

f_vec = np.vectorize(f)

In [470]: f_vec([0, 1, 2, 3], [1, 2, 3, 4])
Out[470]: array([1, 3, 5, 7])

Perfomance:

In [471]: %timeit f_vec([0, 1, 2, 3], [1, 2, 3, 4])
10000 loops, best of 3: 38.3 µs per loop

In [472]: %timeit [f(*par) for par in  zip([0, 1, 2, 3], [1, 2, 3, 4])]
1000000 loops, best of 3: 1.8 µs per loop

In [476]: %timeit list(map(f, [0, 1, 2, 3], [1, 2, 3, 4]))
1000000 loops, best of 3: 1.51 µs per loop

So if you are interested in performance you should use zip and list comprehension or map solution suggested by @tglaria

Sign up to request clarification or add additional context in comments.

5 Comments

Oh, sorry, I used x+y just as an example. The actual calculation in f is more complex so yours may not work in general. But you give some good points. Thanks.
I just realize there is another option: using numpy.vectorize as an annotation of the function.
Why are you using list(map(...)) when map(...) already returns a list?
In python3 map return iterator and if you'd like to convert it to list you need to pass the result to list function
@AntonProtopopov would you look at that. I'm using Python 2.7 so I didn't know that difference in the map function (also, in 3 it stops iterating when the shortest iterable is exhausted).
2

Well, isn't map supposed to be used for this?

def f(x,y):
    return (x,y, x+y)

print map(f,[0,1,2], [-1,0,-2])
>[(0, -1, -1), (1, 0, 1), (2, -2, 0)]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.