【问题标题】:Numpy get values from np.argmin indices [duplicate]Numpy从np.argmin索引中获取值[重复]
【发布时间】:2018-11-05 00:51:52
【问题描述】:

假设我有d1d2d3,如下所示。 t 是一个变量,我在其中组合了我的数组,m 包含最小值的索引。

>>> d1
array([[ 0.9850916 ,  0.95004463,  1.35728604,  1.18554035],
       [ 0.47624542,  0.45561795,  0.6231743 ,  0.94746001],
       [ 0.74008166,  0.        ,  1.59774065,  1.00423774],
       [ 0.86173439,  0.70940862,  1.0601817 ,  0.96112015],
       [ 1.03413477,  0.64874991,  1.27488263,  0.80250053]])
>>> d2
array([[ 0.27301946,  0.38387185,  0.93215524,  0.98851404],
       [ 0.17996978,  0.        ,  0.41283798,  0.15204035],
       [ 0.10952115,  0.45561795,  0.5334015 ,  0.75242805],
       [ 0.4600214 ,  0.74100962,  0.16743427,  0.36250385],
       [ 0.60984208,  0.35161234,  0.44580535,  0.6713633 ]])
>>> d3
array([[ 0.        ,  0.19658541,  1.14605925,  1.18431945],
       [ 0.10697428,  0.27301946,  0.45536417,  0.11922118],
       [ 0.42153386,  0.9850916 ,  0.28225364,  0.82765657],
       [ 1.04940684,  1.63082272,  0.49987388,  0.38596938],
       [ 0.21015723,  1.07007177,  0.22599987,  0.89288339]])
>>> t = np.array([d1, d2, d3])
>>> t
array([[[ 0.9850916 ,  0.95004463,  1.35728604,  1.18554035],
        [ 0.47624542,  0.45561795,  0.6231743 ,  0.94746001],
        [ 0.74008166,  0.        ,  1.59774065,  1.00423774],
        [ 0.86173439,  0.70940862,  1.0601817 ,  0.96112015],
        [ 1.03413477,  0.64874991,  1.27488263,  0.80250053]],

       [[ 0.27301946,  0.38387185,  0.93215524,  0.98851404],
        [ 0.17996978,  0.        ,  0.41283798,  0.15204035],
        [ 0.10952115,  0.45561795,  0.5334015 ,  0.75242805],
        [ 0.4600214 ,  0.74100962,  0.16743427,  0.36250385],
        [ 0.60984208,  0.35161234,  0.44580535,  0.6713633 ]],

       [[ 0.        ,  0.19658541,  1.14605925,  1.18431945],
        [ 0.10697428,  0.27301946,  0.45536417,  0.11922118],
        [ 0.42153386,  0.9850916 ,  0.28225364,  0.82765657],
        [ 1.04940684,  1.63082272,  0.49987388,  0.38596938],
        [ 0.21015723,  1.07007177,  0.22599987,  0.89288339]]])
>>> m = np.argmin(t, axis=0)
>>> m
array([[2, 2, 1, 1],
       [2, 1, 1, 2],
       [1, 0, 2, 1],
       [1, 0, 1, 1],
       [2, 1, 2, 1]])

mt,我想计算实际值如下。我该怎么做呢? ...最好是有效的方法?

array([ [ 0.        ,  0.19658541,  0.93215524,  0.98851404],
        [ 0.10697428,  0.        ,  0.41283798,  0.11922118],
        [ 0.10952115,  0.        ,  0.28225364,  0.75242805],
        [ 0.4600214 ,  0.70940862,  0.16743427,  0.36250385],
        [ 0.21015723,  0.35161234,  0.22599987,  0.6713633 ]])

【问题讨论】:

  • 为什么不使用np.min(t, axis=0),似乎正是你想要的。
  • 谢谢,它对我有用!我需要索引来实现我的算法的某些部分。
  • 看看新的np.take_along _axis

标签: python python-2.7 numpy


【解决方案1】:

如果您只需要最低限度,您可以使用np.min(t, axis=0)

如果要使用习惯索引,可以使用choose

m.choose(t) # This will return the same thing.

也可以写成

np.choose(m, t)

返回:

array([[0.        , 0.19658541, 0.93215524, 0.98851404],
       [0.10697428, 0.        , 0.41283798, 0.11922118],
       [0.10952115, 0.        , 0.28225364, 0.75242805],
       [0.4600214 , 0.70940862, 0.16743427, 0.36250385],
       [0.21015723, 0.35161234, 0.22599987, 0.6713633 ]])

【讨论】:

  • 这太棒了。顺便问一下,如果兴趣轴不为零,你会怎么做?
  • @MadPhysicist 在我的脑海中,可能使用transpose 翻转轴,选择,然后根据需要调整结果数组的轴?
  • 我担心你会这么说:)
  • choose 有 32 个选项的上限。 0
  • @MadPhysicist 哈哈,我的脑细胞在那个上面已经筋疲力尽了:p
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-12
  • 2021-03-24
  • 1970-01-01
  • 2018-02-16
  • 1970-01-01
相关资源
最近更新 更多