Numpy argsort - what is it doing?

后端 未结 9 2386
礼貌的吻别
礼貌的吻别 2020-11-28 19:15

Why is numpy giving this result:

x = numpy.array([1.48,1.41,0.0,0.1])
print x.argsort()

>[2 3 1 0]

when I\'d expect it to do this:

9条回答
  •  粉色の甜心
    2020-11-28 19:50

    Just want to directly contrast the OP's original understanding against the actual implementation with code.

    numpy.argsort is defined such that for 1D arrays:

    x[x.argsort()] == numpy.sort(x) # this will be an array of True's
    

    The OP originally thought that it was defined such that for 1D arrays:

    x == numpy.sort(x)[x.argsort()] # this will not be True
    

    Note: This code doesn't work in the general case (only works for 1D), this answer is purely for illustration purposes.

提交回复
热议问题