例如计算任意行的点积...什么是快速的方法来从两个稀疏矩阵
import numpy as np
from scipy.sparse import csr_matrix
X = csr_matrix([[1,2,3], [4,5,6], [7,8,9]])
Y = csr_matrix([[1,2,3], [4,5,6], [7,8,9], [11,12,13]])
# Print matrices
X.toarray()
[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
Y.toarray()
[[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[11, 12, 13]]
我有一组从X
代表行索引对(X,Y)和从Y
一排。我想采取相应的行的点积,但我无法弄清楚如何有效地做到这一点。
这是我已经试过
# build arbitrary combinations of row from X and row from Y. Need to calculate dot product of each pair
x_idxs = np.array([2,2,1,0])
y_idxs = np.arange(Y.shape[0])
# current method (slow)
def get_dot_product(x_idx, y_idx):
return np.dot(X[x_idx].toarray()[0], Y[y_idx].toarray()[0])
func_args = np.transpose(np.array([x_idxs, y_idxs]))
np.apply_along_axis(func1d=lambda x: get_dot_product(x[0], x[1]), axis=1, arr=func_args)
其作品,但速度很慢作为X
和Y
得到大。有没有更高效的方法?
更新
继沃伦的优雅,但速度慢的解决方案,这里有一个更好的例子进行测试(连同基准)
X = csr_matrix(np.tile(np.repeat(1, 50000),(10000,1)))
Y = X
y_idxs = np.arange(Y.shape[0])
x_idxs = y_idxs
import time
start_time = time.time()
func_args = np.transpose(np.array([x_idxs, y_idxs]))
bg = np.apply_along_axis(func1d=lambda x: get_dot_product(x[0], x[1]), axis=1, arr=func_args)
print("--- %s seconds ---" % (time.time() - start_time)) # 15.48 seconds
start_time = time.time()
ww = X[x_idxs].multiply(Y[y_idxs]).sum(axis=1)
print("--- %s seconds ---" % (time.time() - start_time)) # 38.29 seconds
您是否尝试了Python 2.7和sum(imap(operator.mul,vector1,vector2))[link](https://docs.python.org/2/library/itertools.html)sum(map(operator .mul,vector1,vector2))[link](https://docs.python.org/3/library/itertools.html)适用于Python 3.x – Yunhe
是10000x50000是您正在使用的典型大小吗?您通常计算这些点积的行数是多少? (您更新的示例使用'y_idxs = np.arange(Y.shape [0])' - 换句话说,*所有*行。) –
我正在使用的当前矩阵的维度为X:50K x 120K和Y:250K x 120K,我需要为Y中的每一行计算一个点积(在X中有一些随机行)。我的功能需要6或7分钟才能运行,我怀疑它可以加速很多。 – Ben