Python/Numpy - 从子集获取主数组的索引
假设我有一个包含100个元素的numpy数组。我对这个数组的一个子集进行了一些计算——可能是20个元素,这些元素满足某个条件。然后我在这个子集中选择一个索引,我该如何(高效地)找回在第一个数组中的索引呢?我不想对数组中的所有值进行计算,因为这样很耗费资源,所以我只想在需要的地方(即满足条件的地方)进行计算。
这里有一些伪代码来说明我的意思(这里的“条件”是列表推导式):
a = np.arange(100) # size = 100
b = some_function(a[[i for i in range(0,100,5)]]) # size = 20
Index = np.argmax(b)
# Index gives the index of the maximum value in b,
# but what I really want is the index of the element
# in a
编辑:
我之前说得不太清楚,所以我提供了一个更完整的例子。希望这能更清楚地说明我的目标。我觉得有一种聪明且高效的方法可以做到这一点,而不需要使用循环或查找。
代码:
import numpy as np
def some_function(arr):
return arr*2.0
a = np.arange(100)*2. # size = 100
b = some_function(a[[i for i in range(0,100,5)]]) # size = 20
Index = np.argmax(b)
print Index
# Index gives the index of the maximum value in b, but what I really want is
# the index of the element in a
# In this specific case, Index will be 19. So b[19] is the largest value
# in b. Now, what I REALLY want is the index in a. In this case, that would
# 95 because some_function(a[95]) is what made the largest value in b.
print b[Index]
print some_function(a[95])
# It is important to note that I do NOT want to change a. I will perform
# several calculations on SOME values of a, then return the indices of 'a' where
# all calculations meet some condition.
4 个回答
1
像这样做可以吗?
mask = S == 1
ind_local = np.argmax(X[mask])
G = np.ravel_multi_index(np.where(mask), mask.shape)
ind_global = np.unravel_index(G[ind_local], mask.shape)
return ind_global
这段代码会返回argmax
的全局索引。
1
使用一个辅助数组,叫做 a_index,这个数组里存放的是 a 中元素的索引,比如 a_index[3,5] = (3,5)
。这样你就可以通过 a_index[condition == True][Index]
来获取原来的索引。
如果你能确保 b 是 a 的一个视图(也就是说 b 是 a 的一部分),那么你可以利用这两个数组的 内存布局 信息来找到 b 和 a 的索引之间的对应关系。
2
我不太确定我是否理解你的问题。如果我理解错了,请纠正我。
假设你有这样的东西:
a = np.arange(100)
condition = (a % 5 == 0) & (a % 7 == 0)
b = a[condition]
index = np.argmax(b)
# The following should do what you want
a[condition][index]
或者如果你不想使用遮罩:
a = np.arange(100)
b_indices = np.where(a % 5 == 0)
b = a[b_indices]
index = np.argmax(b)
# Get the value of 'a' corresponding to 'index'
a[b_indices][index]
这就是你想要的吗?