python - What's the difference between numpy.take and numpy.choose? -
it seems numpy.take(array, indices)
, numpy.choose(indices, array)
return same thing: subset of array
indexed indices
.
are there subtle differences between two, or missing more important? , there reason prefer 1 on other?
numpy.take(array, indices)
, numpy.choose(indices, array)
behave on 1-d arrays, coincidence. pointed out jonrsharpe, behave differently on higher-dimensional arrays.
numpy.take
numpy.take(array, indices)
picks out elements flattened version of array
. (the resulting elements of course not same row.)
for example,
numpy.take([[1, 2], [3, 4]], [0, 3])
returns
array([1, 4])
numpy.choose
numpy.choose(indices, set_of_arrays)
plucks out element 0 array indices[0]
, element 1 array indices[1]
, element 2 array indices[2]
, , on. (here, array
set of arrays.)
for example
numpy.choose([0, 1, 0, 0], [[1, 2, 3, 4], [4, 5, 6, 7]])
returns
array([1, 5, 3, 4])
because element 0 comes array 0, element 1 comes array 1, element 2 comes array 0, , element 3 comes array 0.
more information
these descriptions simplified – full descriptions can found here: numpy.take, numpy.choose. example, numpy.take
, numpy.choose
behave when indices
, array
1-d because numpy.choose
first broadcasts array
.
Comments
Post a Comment