Let's assume that there is sigmoid functions that I defined.
def sigmoid(self, x):
return something
I have arrays.
a = np.array([1, 2, 3, 4, 5, 6])
I wanna make "a" like this:
a = [sigmoid(1), sigmoid(2), sigmoid(3), sigmoid(4), sigmoid(5), sigmoid(6)]
Is there anyway that don't use for loops? I mean, some numpy functions?
CodePudding user response:
You can just call the function manually
a = np.array([sigmoid(1),sigmoid(2),..])
or using list-comprehension
a = np.array([sigmoid(i 1) for i in range(6)])
But theres not, as far as I know, a specific numpy function that does it as such
CodePudding user response:
If you want to do this with function
you can use numpy.vectorize
like below:
>>> def sigmoid(x):
... return x 3
>>> a = np.array([1, 2, 3, 4, 5, 6])
>>> vfunc = np.vectorize(sigmoid)
>>> vfunc(a)
array([4, 5, 6, 7, 8, 9])
Or you can do this with lambda
like below:
>>> sigmoid = lambda x: x 3
>>> sigmoid(a)
array([4, 5, 6, 7, 8, 9])