Adrian P. - 1 year ago 134

Python Question

Say I have a function foo() that takes in a single float and returns a single float. What's the fastest/most pythonic way to apply this function to every element in a numpy matrix or array?

What I essentially need is a version of this code that doesn't use a loop:

`import numpy as np`

big_matrix = np.matrix(np.ones((1000, 1000)))

for i in xrange(np.shape(big_matrix)[0]):

for j in xrange(np.shape(big_matrix)[1]):

big_matrix[i, j] = foo(big_matrix[i, j])

I was trying to find something in the numpy documentation that will allow me to do this but I haven't found anything.

Edit: As I mentioned in the comments, specifically the function I need to work with is the sigmoid function,

`f(z) = 1 / (1 + exp(-z))`

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

If `foo`

is really a black box that takes a scalar, and returns a scalar, then you must use some sort of iteration. People often try `np.vectorize`

and realize that, as documented, it does not speed things up much. It is most valuable as a way of broadcasting several inputs. It uses `np.frompyfunc`

, which is slightly faster, but with a less convenient interface.

The proper numpy way is to change your function so it works with arrays. That shouldn't be hard to do with the function in your comments

```
f(z) = 1 / (1 + exp(-z))
```

There's a `np.exp`

function. The rest is simple math.

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**