0

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?

CutePoison
  • 4,679
  • 5
  • 28
  • 63
  • 1
    Checkout [this](https://stackoverflow.com/a/35216364/6645624) answer – K.Mat Nov 26 '21 at 12:33
  • what does your sigmoid function do? – mozway Nov 26 '21 at 13:17
  • Does this answer your question? [numpy apply\_along\_axis on a 1d array](https://stackoverflow.com/questions/32557133/numpy-apply-along-axis-on-a-1d-array) – Ali_Sh Nov 26 '21 at 14:00
  • functions like `apply_along_axis` and `vectorize` are just as slow as loops. `numpy` doesn't have tools to compile your Python function. So whatever you do, it will end up calling `sigmoid` once for each element of `a`. That's where the time consumption is. The "no loops" approach requires writing `sigmoid` itself to work with an array input. – hpaulj Nov 26 '21 at 17:44

1 Answers1

0

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

CutePoison
  • 4,679
  • 5
  • 28
  • 63