简体   繁体   English

scipy.sparse dot在Python中非常慢

[英]scipy.sparse dot extremely slow in Python

The following code will not even finish on my system: 以下代码甚至不会在我的系统上完成:

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)

Is there any explanation why this matrix multiplication is hanging? 有没有解释为什么这个矩阵乘法挂起?

XTdot(L) is not, as you may think, a 50x100 matrix, but an array of 50x100 sparse matrices of 100x100 正如您所想, XTdot(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>

It seems that the problem is that X 's dot method, it being an array, doesn't know about sparse matrices. 似乎问题是Xdot方法,它是一个数组,不知道稀疏矩阵。 So you must either convert the sparse matrix to dense using its todense or toarray method. 因此,您必须使用其todensetoarray方法将稀疏矩阵转换为密集。 The former returns a matrix object, the latter an array : 前者返回一个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]])

Alternatively, sparse matrices have a dot method that knows about arrays: 或者,稀疏矩阵有一个知道数组的dot方法:

>>> X.T.dot(L.dot(X))
array([[  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]])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM