Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 具有邻接矩阵的乘法和点积(numpy)_Python_Numpy_Networkx_Array Broadcasting_Numpy Ufunc - Fatal编程技术网

Python 具有邻接矩阵的乘法和点积(numpy)

Python 具有邻接矩阵的乘法和点积(numpy),python,numpy,networkx,array-broadcasting,numpy-ufunc,Python,Numpy,Networkx,Array Broadcasting,Numpy Ufunc,当我发现以下奇怪之处时,我正在networkx中使用以下代码块。在第一种情况下,我在一个稀疏矩阵上使用了ufunc multiply(*),它意外地正确地给出了一个度序列。然而,当对一个普通矩阵进行同样的处理时,它给了我一个10 x 10的矩阵,正如预期的那样,np.dot(…)给了我正确的结果 import numpy as np import networks as nx ba = nx.barabasi_albert_graph(n=10, m=2) A = nx.adjacency_

当我发现以下奇怪之处时,我正在networkx中使用以下代码块。在第一种情况下,我在一个稀疏矩阵上使用了ufunc multiply(*),它意外地正确地给出了一个度序列。然而,当对一个普通矩阵进行同样的处理时,它给了我一个10 x 10的矩阵,正如预期的那样,np.dot(…)给了我正确的结果

import numpy as np
import networks as nx

ba = nx.barabasi_albert_graph(n=10, m=2)

A = nx.adjacency_matrix(ba)
# <10x10 sparse matrix of type '<class 'numpy.int64'>'
# with 32 stored elements in Compressed Sparse Row format>

A * np.ones(10)

# output: array([ 5.,  3.,  4.,  5.,  4.,  3.,  2.,  2.,  2.,  2.])

nx.degree(ba)

# output {0: 5, 1: 3, 2: 4, 3: 5, 4: 4, 5: 3, 6: 2, 7: 2, 8: 2, 9: 2}

B = np.ones(100).reshape(10, 10)

B * np.ones(10)

array([[ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
   [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
   [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
   [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
   [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
   [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
   [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
   [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
   [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
   [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.]])

np.dot(B, np.ones(10))
# array([ 10.,  10.,  10.,  10.,  10.,  10.,  10.,  10.,  10.,  10.])

这里的细微差别是什么?

对于常规的numpy数组,
*
乘法是元素对元素的乘法(使用
广播
np.dot
是矩阵乘积,乘积之和。对于
np.matrix
子类
*
是矩阵积,
dot
sparse.matrix
不是一个子类,但它是基于该子类建模的<代码>*是矩阵产品

In [694]: A = sparse.random(10,10,.2, format='csr')
In [695]: A
Out[695]: 
<10x10 sparse matrix of type '<class 'numpy.float64'>'
    with 20 stored elements in Compressed Sparse Row format>
In [696]: A *np.ones(10)
Out[696]: 
array([ 0.6349177 ,  0.        ,  1.25781168,  1.12021258,  2.43477065,
        1.10407149,  1.95096264,  0.6253589 ,  0.44242708,  0.50353061])
密集版本:

In [699]: np.dot(A.A,np.ones(10))
Out[699]: 
array([ 0.6349177 ,  0.        ,  1.25781168,  1.12021258,  2.43477065,
        1.10407149,  1.95096264,  0.6253589 ,  0.44242708,  0.50353061])
我认为,
np.dot
应该正确处理稀疏矩阵,这与他们自己的方法不同。但是,
np.dot(A,np.ones(10))
没有做到这一点,生成了由两个稀疏矩阵组成的对象数组。我可以探究原因,但现在,请避免

通常,使用稀疏函数和稀疏矩阵的方法。不要假设
numpy
函数将正确地使用它们


np.dot
在两个数组都是稀疏的情况下工作良好

In [702]: np.dot(A,A)
Out[702]: 
<10x10 sparse matrix of type '<class 'numpy.float64'>'
    with 32 stored elements in Compressed Sparse Row format>
In [703]: np.dot(A,A.T)
Out[703]: 
<10x10 sparse matrix of type '<class 'numpy.float64'>'
    with 31 stored elements in Compressed Sparse Row format>

In [705]: np.dot(A, sparse.csr_matrix(np.ones(10)).T)
Out[705]: 
<10x1 sparse matrix of type '<class 'numpy.float64'>'
    with 9 stored elements in Compressed Sparse Row format>
In [706]: _.A
Out[706]: 
array([[ 0.6349177 ],
       [ 0.        ],
       [ 1.25781168],
       [ 1.12021258],
       [ 2.43477065],
       [ 1.10407149],
       [ 1.95096264],
       [ 0.6253589 ],
       [ 0.44242708],
       [ 0.50353061]])

在本例中,这是一件好事,因为代码看起来更干净,而表达式的两侧可能发生两种截然不同的事情。例如,在我的代码中有一个这样的表达式:“A*np.ones(10)>np.random.uniform(10)*np.ones(10)”。LHS和RHS是不同类型的乘法,但是数学很清楚!
In [699]: np.dot(A.A,np.ones(10))
Out[699]: 
array([ 0.6349177 ,  0.        ,  1.25781168,  1.12021258,  2.43477065,
        1.10407149,  1.95096264,  0.6253589 ,  0.44242708,  0.50353061])
In [702]: np.dot(A,A)
Out[702]: 
<10x10 sparse matrix of type '<class 'numpy.float64'>'
    with 32 stored elements in Compressed Sparse Row format>
In [703]: np.dot(A,A.T)
Out[703]: 
<10x10 sparse matrix of type '<class 'numpy.float64'>'
    with 31 stored elements in Compressed Sparse Row format>

In [705]: np.dot(A, sparse.csr_matrix(np.ones(10)).T)
Out[705]: 
<10x1 sparse matrix of type '<class 'numpy.float64'>'
    with 9 stored elements in Compressed Sparse Row format>
In [706]: _.A
Out[706]: 
array([[ 0.6349177 ],
       [ 0.        ],
       [ 1.25781168],
       [ 1.12021258],
       [ 2.43477065],
       [ 1.10407149],
       [ 1.95096264],
       [ 0.6253589 ],
       [ 0.44242708],
       [ 0.50353061]])
In [708]: A.sum(axis=1)
Out[708]: 
matrix([[ 0.6349177 ],
        [ 0.        ],
        [ 1.25781168],
        [ 1.12021258],
        [ 2.43477065],
        [ 1.10407149],
        [ 1.95096264],
        [ 0.6253589 ],
        [ 0.44242708],
        [ 0.50353061]])