我正在尝试通过 python3 对 numpy 数组进行一些计算。数组: c0 c1 c2 c3r0 1 5 2 7r1 3 9 4 6r2 8 2 1 3这里的“cx”和“rx”是列名和行名。如果元素不在给定的列列表中,我需要逐行计算每个元素的差异。例如 given a column list [0, 2, 1] # they are column indices which means that for r0, we need to calculate the difference between the c0 and all other columns, so we have [1, 5-1, 2-1, 7-1] for r1, we need to calculate the difference between the c2 and all other columns, so we have [3-4, 9-4, 4, 6-4] for r2, we need to calculate the difference between the c1 and all other columns, so we have [8-2, 2, 1-2, 3-2]所以,结果应该是 1 4 1 6 -1 5 4 2 6 2 -1 1因为数组可能非常大,所以我想通过 numpy 向量化操作(例如广播)进行计算。但是,我不确定如何有效地做到这一点。我已经检查了对 numpy 数组的矢量化操作、对 Numpy 切片操作进行矢量化、对大型 NumPy 乘法进行矢量化、用 Numpy 向量化操作替换 For 循环、对循环对 numpy 数组进行矢量化。但是,它们都不适合我。谢谢你的帮助 !
1 回答
喵喵时光机
TA贡献1846条经验 获得超7个赞
先从数组中提取值,然后做减法:
import numpy as np
a = np.array([[1, 5, 2, 7],
[3, 9, 4, 6],
[8, 2, 1, 3]])
cols = [0,2,1]
# create the index for advanced indexing
idx = np.arange(len(a)), cols
# extract values
vals = a[idx]
# subtract array by the values
a -= vals[:, None]
# add original values back to corresponding position
a[idx] += vals
print(a)
#[[ 1 4 1 6]
# [-1 5 4 2]
# [ 6 2 -1 1]]
添加回答
举报
0/150
提交
取消