frazman
frazman

Reputation: 33223

applying a function to each element in vector in numpy

I am writing some performance critical code..

def sig(x):
   return 1/(1+exp(x))

I have a vector

x = [.... say n elements]

transform_x = [sig(ele) for ele in x]

transform_x = map(lambda ele: sig(ele), x) # bit faster than above loop

But is there a better way to apply the function to achieve the above effect but maybe in vectorized way(which I presume should be better)?

Upvotes: 2

Views: 4470

Answers (1)

SmearingMap
SmearingMap

Reputation: 330

You can just apply the function to the entire array, like you would in MATLAB:

transformed = sig(x)

Upvotes: 4

Related Questions