不同的输出在稀疏表示相比于numpy的阵列

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为data.Since的SciPy的稀疏表示我现在处理大数据这就是为什么我想将我的代码转换为稀疏表示,但输出对于相同的输入会有所不同。不同的输出在稀疏表示相比于numpy的阵列

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]]

回答:

你确实使用了的elementwise矩阵乘法在稀疏的一个密集的一,矩阵乘法!

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

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

变化(稀疏版本仅):

Q =-(P*(np.expm1(-A)))   # matrix-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]]

以上是 不同的输出在稀疏表示相比于numpy的阵列 的全部内容, 来源链接: utcz.com/qa/258651.html

回到顶部