group argmax/argmin over partitioning indices in numpy

后端 未结 2 1984
小蘑菇
小蘑菇 2021-01-14 02:30

Numpy\'s ufuncs have a reduceat method which runs them over contiguous partitions within an array. So instead of writing:

import numpy as np
a =         


        
2条回答
  •  佛祖请我去吃肉
    2021-01-14 02:59

    Inspired by this question, ive added argmin/max functionality to the numpy_indexed package. Here is what the corresponding test looks like. Note that the keys may be in any order (and of any kind supported by npi):

    def test_argmin():
        keys   = [2, 0, 0, 1, 1, 2, 2, 2, 2, 2]
        values = [4, 5, 6, 8, 0, 9, 8, 5, 4, 9]
        unique, amin = group_by(keys).argmin(values)
        npt.assert_equal(unique, [0, 1, 2])
        npt.assert_equal(amin,   [1, 4, 0])
    

提交回复
热议问题