What's the difference between numpy.take and numpy.choose?

后端 未结 3 1437
清歌不尽
清歌不尽 2021-02-05 10:34

It seems that numpy.take(array, indices) and numpy.choose(indices, array) return the same thing: a subset of array indexed by indice

3条回答
  •  眼角桃花
    2021-02-05 11:00

    They are certainly not equivalent, as you can see by giving the same arguments (switched) to both methods:

    >>> a = np.array([[1, 2, 3, 4], 
                      [5, 6, 7, 8], 
                      [9, 10, 11, 12], 
                      [13, 14, 15, 16]])
    >>> np.choose([0, 2, 1, 3], a)
    array([ 1, 10,  7, 16]) # one from each row
    >>> np.take(a, [0, 2, 1, 3])
    array([1, 3, 2, 4]) # all from same row
    

    I suggest you read the documentation on take and choose.

提交回复
热议问题