[Numpy-discussion] numpythonically getting elements with the minimum sum

Gregor Thalhammer gregor.thalhammer at gmail.com
Tue Jan 29 03:49:55 EST 2013


Am 28.1.2013 um 23:15 schrieb Lluís:

> Hi,
> 
> I have a somewhat convoluted N-dimensional array that contains information of a
> set of experiments.
> 
> The last dimension has as many entries as iterations in the experiment (an
> iterative application), and the penultimate dimension has as many entries as
> times I have run that experiment; the rest of dimensions describe the features
> of the experiment:
> 
>    data.shape == (... indefinite amount of dimensions ..., NUM_RUNS, NUM_ITERATIONS)
> 
> So, what I want is to get the data for the best run of each experiment:
> 
>    best.shape == (... indefinite amount of dimensions ..., NUM_ITERATIONS)
> 
> by selecting, for each experiment, the run with the lowest total time (sum of
> the time of all iterations for that experiment).
> 
> 
> So far I've got the trivial part, but not the final indexing into "data":
> 
>    dsum = data.sum(axis = -1)
>    dmin = dsum.min(axis = -1)
>    best = data[???]
> 
> 
> I'm sure there must be some numpythonic and generic way to get what I want, but
> fancy indexing is beating me here :)

Did you have a look at the argmin function? It delivers the indices of the minimum values along an axis. Untested guess:

dmin_idx = argmin(dsum, axis = -1)
best = data[..., dmin_idx, :]

Gregor




More information about the NumPy-Discussion mailing list