gpt4 book ai didi

python - 使用 nditer 进行 Numpy 数组比较

转载 作者:行者123 更新时间:2023-12-01 05:44:27 24 4
gpt4 key购买 nike

下面的代码给出了正确的答案,但仅当数组(planmeas)相对较小时才有效。当我尝试在数组上运行它时,我实际上需要比较(每个 300x300),它需要永远(我不知道需要多长时间,因为我在 45 分钟后终止它。)我只想迭代一个范围正在评估的索引周围的数组值 (p)。我尝试查找有关 nditer 标志 'ranged' 的文档,但找不到如何实现要迭代的特定范围。

p = np.nditer(plan, flags = ['multi_index','common_dtype'])
while not p.finished:
gam_store = 100.0
m = np.nditer(meas, flags = ['multi_index','common_dtype'])
while not m.finished:
dis_eval = np.sqrt(np.absolute(p.multi_index[0]-m.multi_index[0])**2 + np.absolute(p.multi_index[1]-m.multi_index[1])**2)
if dis_eval <= 6.0:
a = (np.absolute(p[0] - m[0]) / maxdose) **2
b = (dis_eval / gam_dist) **2
gam_eval = np.sqrt(a + b)
if gam_eval < gam_store:
gam_store = gam_eval
m.iternext()
gamma = np.insert(gamma, location, gam_store, 0)
location = location + 1
p.iternext()

最佳答案

如果您只想迭代数组的一小部分,我认为(除非我误解了这个问题)您应该从数组的一部分创建一个 nditer 实例。

假设您只想要 (i,j) 附近的数组,然后以此开始:

w = 5    # half-size of the window around i, j
p = np.nditer(plan[i-w:i+w, j-w:j+w], flags=...)

这之所以有效,是因为,比如

a = array([[ 0,  1,  2,  3,  4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24]])

那么,

w = 1
i, j = 2,2
print a[i-w:i+w+1, j-w:j+w+1]
#array([[ 6, 7, 8],
# [11, 12, 13],
# [16, 17, 18]])

关于python - 使用 nditer 进行 Numpy 数组比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16551612/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com