大型数组的numpy向量化运算

问题描述

我正在尝试通过python3对numpy数组进行一些计算。

数组:

   c0 c1 c2 c3
r0 1  5  2  7
r1 3  9  4  6
r2 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,1-2,3-2]

所以,结果应该是

   1 4 1 6
   -1 5 4 2
   6 2 -1 1

由于该数组可能非常大,我想通过numpy向量化操作进行计算,例如广播。

BuT,我不确定如何有效地做到这一点。

我已经检查了Vectorizing operation on numpy arrayVectorizing a Numpy slice operationVectorize large NumPy multiplicationReplace For Loop with Numpy Vectorized OperationVectorize numpy array for loop

但是,他们都不适合我。

感谢您的帮助!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)