【问题标题】:Find a minimum value in a column element in a numpy array, and store corresponding row as a new array在numpy数组的列元素中找到最小值,并将对应的行存储为新数组
【发布时间】:2020-03-16 12:53:43
【问题描述】:

我有一个如下所示的 numpy 数组,我想创建一个新的 numpy 数组,该数组的最后一列中的最小值为 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]
...
...]

我知道我们可以使用np.min(xx[:,4]) 在最后一列中得到3.33260017e-01。 但是,我需要提取相应行中的所有值,这将是这个

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

在多维数组中,如xx.shape = (1000,4),如何获取最小值的元素位置?

我的问题是其中之一。

  1. 仅使用最后一列中具有最小值的行创建一个新的 numpy 数组。

  2. 删除除最后一列中具有最小值的行之外的每一行。

【问题讨论】:

  • xx[xx[:, -1].argmin()]?

标签: python arrays numpy


【解决方案1】:

argmin 将为您提供最小值所在的索引。

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

【讨论】:

  • 这比必要的工作多,你只需要在相关列(xx[xx[:, i].argmin()])上做argmin。
【解决方案2】:

一种解决方案:

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

纯 Numpy 解决方案:

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

【讨论】:

  • 请将您的所有代码发布为文本,而不是文本的屏幕截图。图像在不同设备上的格式不一致,并且屏幕阅读器等辅助技术无法解读。
  • 谢谢@Aaron。会修复的。
  • @Kris 你的回答很有帮助!它奏效了!谢谢!
【解决方案3】:

你快到了,你想要np.where

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

【讨论】:

  • np.argmin(xx, axis=-2):更简洁,更少计算。
  • 需要改成:idx=np.where(xx[:,4] == np.min(xx[:,4]))
  • 啊,你说得对,我的测试我在那里用-1,不知道我是如何在这里转换成零的......
猜你喜欢
  • 1970-01-01
  • 2015-03-19
  • 2021-04-15
  • 1970-01-01
  • 2018-09-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-01
相关资源
最近更新 更多