与numpy数组相比,稀疏表示中的输出不同

时间:2017-12-02 22:40:57

标签: python numpy scipy sparse-matrix

import numpy as np
import scipy as sc
from sklearn.preprocessing import normalize
import scipy.sparse as sp
import numpy
import numpy as np
import scipy.sparse as sp




def func1(A,c,eps,maxiter):
    c=0.8
    eps=1e-4
    maxiter=20
    n=sc.shape(A)[0]                   
    sim=sc.eye(n)
    sim_prev=sc.zeros((n,n))    
    I=sc.eye(n)
    P= normalize(A, norm='l1', axis=0)    
    Q=P*(1-sc.exp(-A))        
    for t in range(maxiter):

        sim=c*(sc.dot(Q.T,sim)) + (1-c)*I

    return sim


def func2(A,c,maxiter):
    c=0.8
    eps=1e-4
    maxiter=20
    n=sp.csr_matrix.get_shape(A)[0]    
    sim=sp.eye(n)    
    I=sp.eye(n)
    P= normalize(A, norm='l1', axis=0) 

    Q =-(P*(np.expm1(-A)))

    for t in range(maxiter):


        sim=c*(sc.dot(Q.T,sim)) + (1-c)*I

    return sim

上面给出了两个基本相同的函数,除了func1用于numpy数组,func2用于数据的scipy稀疏表示。因为我正在处理大数据,这就是为什么我想将我的代码转换为稀疏表示但是对于相同的输入,输出会有所不同。

  A=sc.array([[0,1,1,0,1],[1,0,0,1,0],[1,0,0,0,0],[0,1,0,0,0],[1,0,0,0,0]]) #pass this to the func1
    sA = sp.csr_matrix(A)#pass this to func2


output of the sparse func2
(0, 0)        3292.45824232
(0, 3)        777.213797401
(1, 1)        0.798590816646
(1, 2)        0.244114817184
(1, 4)        0.244114817184
(2, 1)        0.244114817184
(2, 2)        0.205180591139
(2, 4)        0.105180591139
(3, 0)        777.213797401
(3, 3)        183.603052715
(4, 1)        0.244114817184
(4, 2)        0.105180591139
(4, 4)        0.205180591139


output of func1

[[ 0.13890945  0.0314584   0.02635767  0.00893873  0.02635767]
 [ 0.04718761  0.12997072  0.00893873  0.03698614  0.00893873]
 [ 0.07907301  0.01787747  0.11498536  0.00510073  0.01498536]
 [ 0.0268162   0.07397228  0.00510073  0.12103198  0.00510073]
 [ 0.07907301  0.01787747  0.01498536  0.00510073  0.11498536]]

1 个答案:

答案 0 :(得分:2)

你确实在稀疏的矩阵乘法中使用了元素矩阵乘法!

归结为A*B在numpy-arrays和scipy.sparse矩阵方面意味着不同的东西。由于这取决于使用的形状,我有点害怕给出一般规则,只是建议阅读numpy和scipy.sparse的文档(简化:A * B = numpy-array的元素乘法,而A. dot(B)矩阵乘法; A * B =稀疏矩阵的矩阵乘法。

更改(仅限稀疏版本):

Q =-(P*(np.expm1(-A)))            # matrix-multiplication

为:

Q =-(P.multiply(np.expm1(-A)))    # elementwise-multiplication

输出:

dense
[[0.25619944 0.04951776 0.04318623 0.01252072 0.04318623]
 [0.07427664 0.24367873 0.01252072 0.06161358 0.01252072]
 [0.12955869 0.02504144 0.22183936 0.00633153 0.02183936]
 [0.03756215 0.12322716 0.00633153 0.23115801 0.00633153]
 [0.12955869 0.02504144 0.02183936 0.00633153 0.22183936]]
sparse
[[0.25619944 0.04951776 0.04318623 0.01252072 0.04318623]
 [0.07427664 0.24367873 0.01252072 0.06161358 0.01252072]
 [0.12955869 0.02504144 0.22183936 0.00633153 0.02183936]
 [0.03756215 0.12322716 0.00633153 0.23115801 0.00633153]
 [0.12955869 0.02504144 0.02183936 0.00633153 0.22183936]]