Donbeo
Donbeo

Reputation: 17617

minimum and argmin in numpy

Given an array I need to find the minimum and the position of the minimum. This can be done using

>>> current_cost
array([ 2.54802261,  2.98627555,  0.23873749,  1.82511195,  1.35469083])
>>> current_cost.min()
0.23873748917821858
>>> current_cost.argmin()
2

This solutions is not very efficient because it needs to scan the list two times. Is there a way to obtain minimum and agrmin at the same time?

Upvotes: 21

Views: 12793

Answers (1)

pv.
pv.

Reputation: 35115

min_pos = current_cost.argmin()
min_val = current_cost[min_pos]

Upvotes: 27

Related Questions