mario119
mario119

Reputation: 353

Find a minimum value in a column element in a numpy array, and store corresponding row as a new array

I have a numpy array like below, and I would like to make a new numpy array which has a row that has the minimum value in the last column which is element of xx[:,4].

xx =
[[6.18167195e-02 -3.20902583e-01  7.96803103e+00   5.69096614e+00  6.82949858e+00]
[1.14139479e-02 -2.93352490e-02  5.17031336e+00   2.50552347e+01  1.51127740e+01]
[8.84761009e-03 -2.93352490e-02  1.84764173e+01   2.50552347e+01  2.17658260e+01]
[1.96567549e-03 -2.93352490e-02  8.18878876e+01   2.50552347e+01  5.34715612e+01]
[3.54827629e-01 -1.88511194e+00  4.70728062e-01   1.95791971e-01  3.33260017e-01]
[3.53146766e-01 -1.88511194e+00  9.42210619e-01   1.95791971e-01  5.69001295e-01]
[6.64244146e-02 -3.20902583e-01  1.10815151e+00   5.69096614e+00  3.39955882e+00]
[6.18167195e-02 -3.20902583e-01  7.96803103e+00   5.69096614e+00  6.82949858e+00]
[1.95005819e-02 -1.40482917e-01  2.64188251e+00   1.63546768e+00  2.13867510e+00]
...
...]

I know that we can use np.min(xx[:,4]) to get 3.33260017e-01 in the last column. However, I need to extract all values in the corresponding row, which will be this

[3.54827629e-01 -1.88511194e+00 4.70728062e-01 1.95791971e-01 3.33260017e-01].

In a multidimensional array, such as xx.shape = (1000,4), how can I get the element locations of the minimum value?

My question is either one of them.

  1. Make a new numpy array only with the row that has the minimum value in the last column.

  2. Delete every rows except the row that has the minimum value in the last column.

Upvotes: 0

Views: 1534

Answers (3)

Ben Taylor
Ben Taylor

Reputation: 515

You're nearly there, you want np.where

idx = np.where(xx[:, -1] == np.min(xx[:,4]))
output = xx[idx]

Upvotes: 1

Brad Campbell
Brad Campbell

Reputation: 3071

argmin will give you the index where the minimum value is.

>>> xx=np.array([[2,1,3],[1,0,2],[3,3,1]])
>>> xx
array([[2, 1, 3],
       [1, 0, 2],
       [3, 3, 1]])
>>> column = 1
>>> i = xx[:,column].argmin()
1
>>> xx[i,:]  # get the row where column 1 has the minimum
array([1, 0, 2])

Upvotes: 2

Kris
Kris

Reputation: 420

One solution:

>>> import numpy as np
>>>
>>> xx = [[6.18167195e-02,-3.20902583e-01,7.96803103e+00,5.69096614e+00,6.82949858e+00],
... [1.14139479e-02,-2.93352490e-02,5.17031336e+00,2.50552347e+01,1.51127740e+01],
... [8.84761009e-03,-2.93352490e-02,1.84764173e+01,2.50552347e+01,2.17658260e+01],
... [1.96567549e-03,-2.93352490e-02,8.18878876e+01,2.50552347e+01,5.34715612e+01],
... [3.54827629e-0,-1.88511194e+00,4.70728062e-01,1.95791971e-01,3.33260017e-01],
... [3.53146766e-01,-1.88511194e+00,9.42210619e-01,1.95791971e-01,5.69001295e-01],
... [6.64244146e-02,-3.20902583e-01,1.10815151e+00,5.69096614e+00,3.39955882e+00],
... [6.18167195e-02,-3.20902583e-01,7.96803103e+00,5.69096614e+00,6.82949858e+00],
... [1.95005819e-02,-1.40482917e-01,2.64188251e+00,1.63546768e+00,2.13867510e+00]]
>>>
>>> xx = np.asarray(xx)
>>>
>>> def MinRow(array):
...     low = np.min(array[:,4])
...     for idx, el in enumerate(array):
...         if el[-1] <= low:
...             index = idx
...     newarray = array[index, :]
...     return newarray
...
>>> xx2 = MinRow(xx)
>>> print(xx2)
[ 3.54827629 -1.88511194  0.47072806  0.19579197  0.33326002]

Pure Numpy Solution:

>>> import numpy as np
>>>
>>> xx = [[6.18167195e-02,-3.20902583e-01,7.96803103e+00,5.69096614e+00,6.82949858e+00],
... [1.14139479e-02,-2.93352490e-02,5.17031336e+00,2.50552347e+01,1.51127740e+01],
... [8.84761009e-03,-2.93352490e-02,1.84764173e+01,2.50552347e+01,2.17658260e+01],
... [1.96567549e-03,-2.93352490e-02,8.18878876e+01,2.50552347e+01,5.34715612e+01],
... [3.54827629e-0,-1.88511194e+00,4.70728062e-01,1.95791971e-01,3.33260017e-01],
... [3.53146766e-01,-1.88511194e+00,9.42210619e-01,1.95791971e-01,5.69001295e-01],
... [6.64244146e-02,-3.20902583e-01,1.10815151e+00,5.69096614e+00,3.39955882e+00],
... [6.18167195e-02,-3.20902583e-01,7.96803103e+00,5.69096614e+00,6.82949858e+00],
... [1.95005819e-02,-1.40482917e-01,2.64188251e+00,1.63546768e+00,2.13867510e+00]]
>>>
>>> xx = np.asarray(xx)
>>> idx = np.where(xx[:,4]==np.min(xx[:,4]))
>>> xx2 = xx[idx]
>>> print(xx2)
[[ 3.54827629 -1.88511194  0.47072806  0.19579197  0.33326002]]

Upvotes: 1

Related Questions