I'm trying to convert some code from Matlab to Python. Any idea how I would go about converting this line? I'm very new to python and I've never seen arrayfun before. Thanks. Much appreciated.
zj=arrayfun(@sigmoid,aj);
A generic way, use a loop:
zj=[sigmoid(x) for x in aj]
You will want to use the numerical library numpy whenever you're working with numerical data.
In it, the Matlab function called arrayfun is simply the vectorized form of that function. E.g.
Matlab:
>> a = 1:4
a =
1 2 3 4
>> arrayfun(@sqrt, a)
ans =
1.0000 1.4142 1.7321 2.0000
>> sqrt(a)
ans =
1.0000 1.4142 1.7321 2.0000
Whereas in numpy, you'd do:
>>> import numpy as np
>>> a = np.arange(4)
>>> np.sqrt(a)
array([ 0. , 1. , 1.41421356, 1.73205081])
Most functions can be vectorized, a sigmoid is no exception to that. For example, if the sigmoid were defined as 1./(1 + exp(-x)), then you could write in Python:
def sigmoid(x):
return 1./(1 + np.exp(-x))
zj = sigmoid(aj)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With