问题描述
numpy.take(array, indices)
和numpy.choose(indices, array)
似乎返回相同的东西:由indices
索引的array
的子集.
It seems that numpy.take(array, indices)
and numpy.choose(indices, array)
return the same thing: a subset of array
indexed by indices
.
两者之间只有细微的差别吗,还是我缺少更重要的东西?并且有理由偏爱另一个吗?
Are there only subtle differences between the two, or am I missing something more important? And is there a reason to prefer one over the other?
推荐答案
numpy.take(array, indices)
和numpy.choose(indices, array)
在一维数组上的行为类似,但这只是巧合.正如jonrsharpe指出的那样,它们在高维数组上的行为有所不同.
numpy.take(array, indices)
and numpy.choose(indices, array)
behave similarly on 1-D arrays, but this is just coincidence. As pointed out by jonrsharpe, they behave differently on higher-dimensional arrays.
numpy.take(array, indices)
从平坦版本的array
中挑选元素. (结果元素当然不一定来自同一行.)
numpy.take(array, indices)
picks out elements from a flattened version of array
. (The resulting elements are of course not necessarily from the same row.)
例如,
numpy.take([[1, 2], [3, 4]], [0, 3])
返回
array([1, 4])
numpy.choose
numpy.choose(indices, set_of_arrays)
从数组indices[0]
中抽出元素0,从数组indices[1]
中抽出元素1,从数组indices[2]
中抽出元素2,依此类推. (这里,array
实际上是一组数组.)
numpy.choose
numpy.choose(indices, set_of_arrays)
plucks out element 0 from array indices[0]
, element 1 from array indices[1]
, element 2 from array indices[2]
, and so on. (Here, array
is actually a set of arrays.)
例如
numpy.choose([0, 1, 0, 0], [[1, 2, 3, 4], [4, 5, 6, 7]])
返回
array([1, 5, 3, 4])
因为元素0来自数组0,元素1来自数组1,元素2来自数组0,元素3来自数组0.
because element 0 comes from array 0, element 1 comes from array 1, element 2 comes from array 0, and element 3 comes from array 0.
这些描述得到了简化–完整的描述可以在这里找到: numpy.take , numpy.choose .例如,当indices
和array
为一维时,numpy.take
和numpy.choose
的行为类似,因为numpy.choose
首先广播array
.
These descriptions are simplified – full descriptions can be found here: numpy.take, numpy.choose. For example, numpy.take
and numpy.choose
behave similarly when indices
and array
are 1-D because numpy.choose
first broadcasts array
.
这篇关于numpy.take和numpy.choose有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!