python - 在Python中按索引对矩阵元素进行求和
问题描述
我有两个矩阵(相同的行和列):一个带有浮点值,它们按另一个矩阵中的索引分组。因此,我想要一个字典或列表,其中包含每个索引的元素总和。索引始终从 0 开始。
A = np.array([[0.52,0.25,-0.45,0.13],[-0.14,-0.41,0.31,-0.41]])
B = np.array([[1,3,1,2],[3,0,2,2]])
RESULT = {0: -0.41, 1: 0.07, 2: 0.03, 3: 0.11}
我找到了这个解决方案,但我正在寻找一个更快的解决方案。我正在使用 784 x 300 单元格的矩阵,这个算法需要大约 28 毫秒才能完成。
import numpy as np
def matrix_sum_by_indices(indices,matrix):
a = np.hstack(indices)
b = np.hstack(matrix)
sidx = a.argsort()
split_idx = np.flatnonzero(np.diff(a[sidx])>0)+1
out = np.split(b[sidx], split_idx)
return [sum(x) for x in out]
如果您能帮助我找到更好、更简单的解决方案,我将不胜感激!
编辑:我犯了一个错误,在 300*10 矩阵中完成时间约为 8ms,但在 784x300 中完成时间约为 28ms。
EDIT2:我的A
元素是 float64,所以bincount
给我 ValueError。
解决方案
您可以在bincount
这里使用:
a = np.array([[0.52,0.25,-0.45,0.13],[-0.14,-0.41,0.31,-0.41]])
b = np.array([[1,3,1,2],[3,0,2,2]])
N = b.max() + 1
id = b + (N*np.arange(b.shape[0]))[:, None] # since you can't apply bincount to a 2D array
np.sum(np.bincount(id.ravel(), a.ravel()).reshape(a.shape[0], -1), axis=0)
输出:
array([-0.41, 0.07, 0.03, 0.11])
作为一个函数:
def using_bincount(indices, matrx):
N = indices.max() + 1
id = indices + (N*np.arange(indices.shape[0]))[:, None] # since you can't apply bincount to a 2D array
return np.sum(np.bincount(id.ravel(), matrx.ravel()).reshape(matrx.shape[0], -1), axis=0)
此示例的计时:
In [5]: %timeit using_bincount(b, a)
31.1 µs ± 1.74 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
In [6]: %timeit matrix_sum_by_indices(b, a)
61.3 µs ± 2.62 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
In [88]: %timeit scipy.ndimage.sum(a, b, index=[0,1,2,3])
54 µs ± 218 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
(scipy.ndimage.sum
在更大的样本上应该更快)
推荐阅读
- raspberry-pi3 - IOT Hub 和 Raspberry pi 3 之间的数据传输问题
- linux - 如何在 Linux Bash 中将变量内的值相加?
- jsf - 当按下“Enter”键时,如何让按钮“按下”提交输入文本?
- mysql - MYSQL JOIN 同一张表,合并空字段并求和
- azure - Securing assets on Microsoft Azure CDN (Verizon Premium) using token authentication
- google-sheets - Find row number of previous instance of string in google sheets
- php - A href links in PHP include file are not relative to the root
- cordova - Ugly image before Youtube video starts playing in Cordova android app
- java - Sending HttpServletrequest to faces servlet
- javascript - How to store and retrieve an undefined guid with Javascript?