Search code examples
pythonnumpysliceargmax

Find indices for max values subarrays and applying it on that subarray


I have a file f which holds N (unknown) events. Each event carries an (unknown and different for each event, call it i, j etc) amount of reconstructed tracks. Then, each track has properties like energy E and likelihood lik. So,

>>> print(f.events.tracks.lik) 
[[lik1, lik2, ..., likX], [lik1, lik2, ..., likj], ..., [lik1, lik2, ..., likz]]

prints an array holding N subarrays (1 per event), each presenting the lik for all its tracks.

GOAL: call f.events.tracks[:, Inds].E to get the energies for the tracks with max likelihood.

Minimal code example

>>>import numpy as np
>>>lik = np.random.randint(low=0, high=100, size=50).reshape(5, 10)
>>>print(lik)
[[ 3 49 27  3 80 59 96 99 84 34]
 [88 62 61 83 90  9 62 30 92 80]
 [ 5 21 69 40  2 40 13 63 42 46]
 [ 0 55 71 67 63 49 29  7 21  7]
 [40  7 68 46 95 34 74 88 79 15]]
>>>energy = np.random.randint(low=100, high=2000, size=50).reshape(5, 10)
>>>print(energy)
[[1324 1812  917  553  185  743  358  877 1041  905]
[1407  663  359  383  339 1403 1511 1964 1797 1096]
[ 315 1431  565  786  544 1370  919 1617 1442  925]
[1710  698  246 1631 1374 1844  595  465  908  953]
[ 305  384  668  952  458  793  303  153  661  791]]
>>> Inds = np.argmax(lik, axis=1)
>>> print(Inds)
[2 1 8 6 7]

PROBLEM:

>>> # call energy[Inds] to get
# [917, 663, 1442, 1844, 153]

What is the correct way of accessing these energies?


Solution

  • You can select the values indexed by Inds for each line using a 2D indexing with a temporary array containing [0,1,2,...] (generated using np.arange).

    Here is an example:

    energy[np.arange(len(Inds)), Inds]