简体   繁体   English

从行索引,列索引和max(values)的np.array填充矩阵的快速方法

[英]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.

相关问题 Python:将 CSV 列解析为 np.array 的行和列索引? - Python: Parse a CSV column as a row and column index for a np.array? 从pandas数据框创建np.array,该数据框有一列保存数组索引的值,另一列保存每个索引的值? - Create np.array from pandas dataframe which has a column holding values of the array's indices and another column holding the value at each index? 用户定义的索引用np.nan替换np.array值的最简洁方法 - Cleanest way to replace np.array value with np.nan by user defined index 在numpy矩阵中查找最大列值的行索引 - Finding row index of max column values in a numpy matrix 如何在 np.array 的特定范围内找到最大值的索引? - How do I find the index of the max value in a specific range of np.array? 有没有办法让列表处理和 np.array 一样快? - Is there a way to make list processing as fast as np.array? 有没有一种快速的方法来替换只包含 1 和 -1 的 np.array 的元素 - Is there a fast way of replacing elements of a np.array that contain only 1 and -1 如何使用Python中的索引列表索引np.array - How to index an np.array with a list of indices in Python 数据帧到np.array-IndexError:元组索引超出范围 - dataframe to np.array - IndexError: tuple index out of range 如何在保留索引的同时将Pandas数据帧转换为np.array? - How to convert Pandas dataframe to np.array while preserving the index?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM