1

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)
3
  • May this solution help stackoverflow.com/a/8079151/5050917 ? Commented Jan 17, 2016 at 2:40
  • What is the relation between vector and vector_length? Commented Jan 18, 2016 at 8:37
  • Vector is an array of numbers and vector_length is its length. It is actually of no importance. Method call is the essence :D Commented Jan 18, 2016 at 21:37

1 Answer 1

1

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).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.