0

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.

1
  • 1
    xx[xx[:, -1].argmin()]? Commented Nov 20, 2019 at 18:31

3 Answers 3

2

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])
Sign up to request clarification or add additional context in comments.

1 Comment

This does more work than necessary, you only need to do argmin on the relevant column (xx[xx[:, i].argmin()]).
1

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]]

3 Comments

Please post all your code as text rather than screenshots of text. Images are not consistently formatted correctly across devices, and are impossible for assistive technologies such as screen readers to interpret.
Thanks @Aaron. Will fix.
@Kris Your answer was very helpful! and it worked! Thank you!
1

You're nearly there, you want np.where

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

3 Comments

np.argmin(xx, axis=-2): more concise, and less computation.
need to change to: idx=np.where(xx[:,4] == np.min(xx[:,4]))
Ah, you're right, my test I used -1 there, not sure how I converted to a zero here...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.