Dusan J.
Dusan J.

Reputation: 303

How to efficiently compute function for every cell in numpy array?

I guess that iterating over an numpy array is not the most efficient way, and I can see that my program is really slow now that I have a bit larger dataset.

1) What is the go to way to iterate over a matrix and apply a function to each cell?

This is part of the code:

# States and data are two lists with a few appended items ~100
rows = len(self.states)
cols = len(self.data)
self.trellis = np.zeros((rows, cols))
    for i, state in enumerate(self.states):
        for j, vector in enumerate(self.data):
            self.trellis[i][j] = mvnun_wrapper(vector, state.mu, state.sigma, vector_length)

Upvotes: 1

Views: 256

Answers (1)

B. M.
B. M.

Reputation: 18628

it seems to be a classic numpy problem. states sound like a list of state with 2 attributes, mu and sigma.

I don't think vector_length is requisite here, and suppose mvnun a function of three scalars.

then just try:

mu = [state.mu for state in states]
sigma = [state.sigma for state in states]
v=np.asarray(vector).reshape(-1,1) # a "column" vector
result = mvnun(v,mu,sigma)

As an example :

class state():
    def __init__(self):
        self.mu=np.random.random()
        self.sigma=np.random.random() 

states=[state() for _ in range(10)]  # 10 states
vector=list(range(5))  # a 5-vector
def mvnun(x,m,s) : return x*m+3*x*s # a scalar function

mu=[state.mu for state in states]
sigma = [state.sigma for state in states]
v=np.asarray(vector).reshape(-1,1) # a "column" vector
result = mvnun(v,mu,sigma)

result.shapeis (5,10).

Upvotes: 1

Related Questions