[英]Fast way to fill matrix from np.array of row index, column index, and max(values)
I have quite large arrays to fill matrix (about 5e6
elements). 我有很大的数组来填充矩阵(大约5e6
元素)。 I know the fast way to fill is something like 我知道填充的快速方法是
(simplified example) (简化示例)
bbb = (np.array([1,2,3,4,1])) # row
ccc = (np.array([0,1,2,1,0])) # column
ddd = (np.array([55.5,22.2,33.3,44.4,11.1])) # values
experiment = np.zeros(shape=(5,3))
experiment[bbb, ccc] = [ddd] # filling
>[[ 0. 0. 0. ]
[ 11.1 0. 0. ]
[ 0. 22.2 0. ]
[ 0. 0. 33.3]
[ 0. 44.4 0. ]]
but if I want the max ddd
instead. 但是如果我想要最大ddd
代替。 Something like at # filling
像# filling
#pseudocode
experiment[bbb, ccc] = [ddd if ddd > experiment[bbb, ccc]]
The matrix should return 矩阵应返回
>[[ 0. 0. 0. ]
[ 55.5 0. 0. ]
[ 0. 22.2 0. ]
[ 0. 0. 33.3]
[ 0. 44.4 0. ]]
What is a good fast way to get max to fill the matrix from np.array here? 在这里从np.array获取最大值以填充矩阵的快速方法是什么?
You can use np.ufunc.at
on np.maximum
. 您可以在np.ufunc.at
上使用np.maximum
。
np.ufunc.at
performs the preceding ufunc
"unbuffered and in-place". np.ufunc.at
执行前述ufunc
“非缓冲和就地”。 This means all indices appearing in [bbb, ccc]
will be processed by np.maximum
, no matter how ofthen those indices appear. 这意味着出现在[bbb, ccc]
所有索引将由np.maximum
处理,无论这些索引如何出现。
In your case (0, 1)
appears twice, so it will be processed twice, each time picking the maximum of experiment[bbb, ccc]
and ddd
. 在您的情况下(0, 1)
出现两次,因此将处理两次,每次选择最大的experiment[bbb, ccc]
和ddd
。
np.maximum.at(experiment, [bbb, ccc], ddd)
# array([[ 0. , 0. , 0. ],
# [ 55.5, 0. , 0. ],
# [ 0. , 22.2, 0. ],
# [ 0. , 0. , 33.3],
# [ 0. , 44.4, 0. ]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.