16

I have a filter expression as follows:

feasible_agents = filter(lambda agent: agent >= cost[task, agent], agents)

where agents is a python list.

Now, to get speedup, I am trying to implement this using numpy.

What would be the equivalent using numpy?

I know that this works:

threshold = 5.0
feasible_agents = np_agents[np_agents > threshold]

where np_agents is the numpy equivalent of agents.

However, I want threshold to be a function of each element in the numpy array.

3
  • "I want threshold to be a function of each element in the numpy array." Please explain. Commented Sep 18, 2018 at 7:22
  • As you can see from the first expression using lambda, the threshold is the cost value that is obtained by lookup in the cost table using each element of the agents list as key. task is a constant here. i.e. the threshold is not fixed, but will vary for every element agent in the numpy array np_agents. Commented Sep 18, 2018 at 7:26
  • Difficult to vectorize a solution involving a dictionary. Maybe provide a minimal reproducible example. If the keys to your dict are integers, something may be possible here. Commented Sep 18, 2018 at 7:36

2 Answers 2

15

You can use numpy.extract:

>>> nparr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
>>> nparreven = np.extract(nparr % 2 == 0, nparr)

or numpy.where:

>>> nparr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
>>> nparreven = nparr[np.where(nparr % 2 == 0)]
Sign up to request clarification or add additional context in comments.

Comments

8

Since you don't provide an example data, use toy data:

# Cost of agents represented by indices of cost, we have agents 0, 1, 2, 3
cost = np.array([4,5,6,2])
# Agents to consider 
np_agents = np.array([0,1,3])
# threshold for each agent. Calculate different thresholds for different agents. Use array of indexes np_agents into cost array.
thresholds = cost[np_agents] # np.array([4,5,2])
feasible_agents = np_agents[np_agents > thresholds] # np.array([3])

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.