scipy.sparse dot在Python中非常慢

时间:2013-01-07 21:32:57

标签: python numpy scipy sparse-matrix

以下代码甚至无法在我的系统上完成:

import numpy as np
from scipy import sparse
p = 100
n = 50
X = np.random.randn(p,n)
L = sparse.eye(p,p, format='csc')
X.T.dot(L).dot(X)

有没有解释为什么这个矩阵乘法会挂起?

1 个答案:

答案 0 :(得分:9)

正如您所想,

X.T.dot(L)不是50x100矩阵,而是50x100稀疏矩阵的数组100x100

>>> X.T.dot(L).shape
(50, 100)
>>> X.T.dot(L)[0,0]
<100x100 sparse matrix of type '<type 'numpy.float64'>'
    with 100 stored elements in Compressed Sparse Column format>

问题似乎是X的{​​{1}}方法,它是一个数组,不知道稀疏矩阵。因此,您必须使用其dottodense方法将稀疏矩阵转换为密集。前者返回toarray个对象,后者为matrix

array

或者,稀疏矩阵具有知道数组的>>> X.T.dot(L.todense()).dot(X) matrix([[ 81.85399873, 3.75640482, 1.62443625, ..., 6.47522251, 3.42719396, 2.78630873], [ 3.75640482, 109.45428475, -2.62737229, ..., -0.31310651, 2.87871548, 8.27537382], [ 1.62443625, -2.62737229, 101.58919604, ..., 3.95235372, 1.080478 , -0.16478654], ..., [ 6.47522251, -0.31310651, 3.95235372, ..., 95.72988689, -18.99209596, 17.31774553], [ 3.42719396, 2.87871548, 1.080478 , ..., -18.99209596, 108.90045569, -16.20312682], [ 2.78630873, 8.27537382, -0.16478654, ..., 17.31774553, -16.20312682, 105.37102461]]) 方法:

dot