I can’t seem to get my head around how to do this – I am new to Python and this kind of work with arrays. I have a large array, say:
JavaScript
x
8
1
array([[119., 323., 42., 277., 401.],
2
[122., 326., 39., 278., 10.],
3
[125., 329., 36., 12., 407.],
4
,
5
[308., 314., 469., 188., 266.],
6
[308., 314., 469., 188., 266.],
7
[308., 314., 469., 188., 266.]])
8
I would like to find the column index of the minimum value in each row. For instance for the first 3 rows would give [2, 4, 3….]. I have experimented with .min() and np.where(), for instance:
JavaScript
1
2
1
np.where(array == array.min())
2
But I just can’t seem to get the answer I’m looking for. Any help would be much appreciated, thanks
Advertisement
Answer
Use numpy argmin()
:
JavaScript
1
2
1
np.argmin(a, axis=1)
2
where a
is your numpy array.