似乎numpy.take(array, indices)
和numpy.choose(indices, array)
返回相同的内容:由array
索引的indices
的子集。
这两者之间是否存在微妙的差异,还是我错过了一些更重要的东西?是否有理由偏爱另一个?
答案 0 :(得分:22)
numpy.take(array, indices)
和numpy.choose(indices, array)
在1-D阵列上表现相似,但这只是巧合。正如jonrsharpe所指出的,它们在高维数组上表现不同。
numpy.take(array, indices)
从展平版array
中挑选出元素。 (结果元素当然不一定来自同一行。)
例如,
numpy.take([[1, 2], [3, 4]], [0, 3])
返回
array([1, 4])
numpy.choose(indices, set_of_arrays)
从数组indices[0]
中取出元素0,从数组indices[1]
中取出元素1,从数组indices[2]
中取出元素2,依此类推。 (这里,array
实际上是一组数组。)
例如
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。
这些说明已经过简化 - 完整说明可在此处找到:numpy.take,numpy.choose。例如,当numpy.take
和numpy.choose
为1-D时,indices
和array
的行为相似,因为numpy.choose
首先广播array
。
答案 1 :(得分:6)
它们当然不是等价的,正如你可以通过给两个方法提供相同的参数(切换)看到的那样:
>>> 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
答案 2 :(得分:0)
文档中对 np.choose()
行为的解释。
np.choose(a,c) == np.array([c[a[I]][I] for I in ndi.ndindex(a.shape)])
import numpy as np
print("--------------------------------------------------------------------------------")
print("Index array 'a' and array of choice(s).")
print("--------------------------------------------------------------------------------")
a = np.array([4, 3, 2, 1])
print("'a' is {} shape is {}\n".format(
a, a.shape
))
c = np.arange(60).reshape((5, 3, 4))
_choice = c[0]
print("An example choice is \n{} \nshape is {}\n".format(
_choice,
_choice.shape
))
--------------------------------------------------------------------------------
Index array 'a' and array of choice(s).
--------------------------------------------------------------------------------
'a' is [4 3 2 1] shape is (4,)
An example choice is
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
shape is (3, 4)
给定一个由整数组成的“索引”数组 (a) 和一个由 n 个数组组成的序列
(choices), a 和每个选择数组首先广播,必要时,
到共同形状的数组;调用这些 Ba 和 Bchoices[i], i =
0,...,n-1 我们有,必然,Ba.shape == Bchoices[i].shape
为
每个我。
Ba.shape == Bchoices[i].shape
。
print("--------------------------------------------------------------------------------")
print("np.choose() broadcasts 'a' and 'choice' to match (here only on a)")
print("--------------------------------------------------------------------------------")
a = np.vstack([
np.array(a) for i in range(_choice.shape[0])
])
print("Broadcast shape of 'a' is {}\n 'a' is \n{}\n.".format(a.shape, a))
--------------------------------------------------------------------------------
np.choose() broadcasts 'a' and 'choice' to match (here only on a)
--------------------------------------------------------------------------------
Broadcast shape of 'a' is (3, 4)
'a' is
[[4 3 2 1]
[4 3 2 1]
[4 3 2 1]]
c[a[I]][I] for I in ndi.ndindex(a.shape)
假设 i(在那个范围内)是 (j0, j1, …, jm) 处的值 Ba 中的位置 - 然后是新数组中相同位置的值 是 Bchoices[i] 中相同位置的值;
print("--------------------------------------------------------------------------------")
print("Simulate np.choose() behavior of choosing elements c[a[I]][I]]]:")
print("np.array([ c[a[I]][I] for I in np.lib.index_tricks.ndindex(a.shape) ])")
print("--------------------------------------------------------------------------------")
result = np.array([]).astype(int)
for I in np.lib.index_tricks.ndindex(a.shape):
print("Selecting the element {} at c[{},{}]".format(
c[a[I]][I].astype(int), a[I], I
))
result = np.concatenate([result, [c[a[I]][I].astype(int)]])
print("chosen items: {}".format(result))
print("\nResult is \n{}\n".format(
result.reshape(a.shape)
))
--------------------------------------------------------------------------------
Simulate np.choose() behavior of choosing elements c[a[I]][I]]]:
np.array([ c[a[I]][I] for I in np.lib.index_tricks.ndindex(a.shape) ])
--------------------------------------------------------------------------------
Selecting the element 48 at c[4,(0, 0)]
chosen items: [48]
Selecting the element 37 at c[3,(0, 1)]
chosen items: [48 37]
Selecting the element 26 at c[2,(0, 2)]
chosen items: [48 37 26]
Selecting the element 15 at c[1,(0, 3)]
chosen items: [48 37 26 15]
Selecting the element 52 at c[4,(1, 0)]
chosen items: [48 37 26 15 52]
Selecting the element 41 at c[3,(1, 1)]
chosen items: [48 37 26 15 52 41]
Selecting the element 30 at c[2,(1, 2)]
chosen items: [48 37 26 15 52 41 30]
Selecting the element 19 at c[1,(1, 3)]
chosen items: [48 37 26 15 52 41 30 19]
Selecting the element 56 at c[4,(2, 0)]
chosen items: [48 37 26 15 52 41 30 19 56]
Selecting the element 45 at c[3,(2, 1)]
chosen items: [48 37 26 15 52 41 30 19 56 45]
Selecting the element 34 at c[2,(2, 2)]
chosen items: [48 37 26 15 52 41 30 19 56 45 34]
Selecting the element 23 at c[1,(2, 3)]
chosen items: [48 37 26 15 52 41 30 19 56 45 34 23]
Result is
[[48 37 26 15]
[52 41 30 19]
[56 45 34 23]]
print("--------------------------------------------------------------------------------")
print("Run np.choose(a, c):")
print("--------------------------------------------------------------------------------")
print(np.choose(a, c))
--------------------------------------------------------------------------------
Run np.choose(a, c):
--------------------------------------------------------------------------------
[[48 37 26 15]
[52 41 30 19]
[56 45 34 23]]