Faster alternatives to numpy.argmax/argmin which is slow

前端 未结 3 899
深忆病人
深忆病人 2020-11-29 11:53

I am using a lot of argmin and argmax in Python.

Unfortunately, the function is very slow.

I have done some searching around, and the best I can find is here

3条回答
  •  南方客
    南方客 (楼主)
    2020-11-29 12:20

    For those that came for a short numpy-free snippet that returns the index of the first minimum value:

    def argmin(a):
        return min(range(len(a)), key=lambda x: a[x])
    a = [6, 5, 4, 1, 1, 3, 2]
    argmin(a)  # returns 3
    

提交回复
热议问题