【发布时间】:2020-03-29 23:31:42
【问题描述】:
我有一个大的二维数组,我可以通过索引访问它。我只想更新索引数组中不为零的值。
arrayx = np.random.random((10,10))
假设我有索引(这只是示例,实际索引是由单独的进程生成的):
idxs = np.array([[4],
[5],
[6],
[7],
[8]]), np.array([[5, 9]])
鉴于这些索引,这应该可以工作,但它没有。
arrayx[idxs]
array([[0.7 , 0.1 ],
[0.79, 0.51],
[0. , 0.8 ],
[0.82, 0.32],
[0.82, 0.89]], dtype=float16)
// note from editor: '<>' is equivalent to '!='
// but I agree that '>' 0 is more correct
// mask = mapx[idxs] <> 0 // original
mask = arrayx[idxs] > 0 // better
array([[ True, True],
[ True, True],
[False, True],
[ True, True],
[ True, True]])
arrayx[idxs][mask] += 1
但是,这不会更新数组。我该如何解决这个问题?
【问题讨论】:
-
所以你的地图的 2D 和索引有一个奇怪的形状你能用适当的数据更新你的问题吗?我的意思是你有一些可以理解的地图,但不是 idxs
-
另外,你应该避免使用
map作为变量名,因为它会覆盖python的内置映射函数 -
您的目标是更新
mapx本身吗?
标签: python numpy indexing updates mask