Python Scipy给出了错误的矩阵乘法结果

Python Scipy给出了错误的矩阵乘法结果,python,numpy,matrix,scipy,matrix-multiplication,Python,Numpy,Matrix,Scipy,Matrix Multiplication,我正在使用scipy对稀疏矩阵进行矩阵乘法。由于某些原因,.power()方法不适用于稀疏矩阵。我用三种方法进行了检查: 这是我的密码: import scipy as sp import scipy.sparse 方法1:普通矩阵乘法 row = np.array([0, 3, 1, 0]) col = np.array([0, 3, 1, 2]) data = np.array([4, 5, 7, 9]) P1 = sp.sparse.coo_matrix((data, (row, co

我正在使用
scipy
对稀疏矩阵进行矩阵乘法。由于某些原因,
.power()
方法不适用于稀疏矩阵。我用三种方法进行了检查:

这是我的密码:

import scipy as sp
import scipy.sparse 
方法1:普通矩阵乘法

row = np.array([0, 3, 1, 0])
col = np.array([0, 3, 1, 2])
data = np.array([4, 5, 7, 9])
P1 = sp.sparse.coo_matrix((data, (row, col)), shape=(4, 4))
#Method 1
P1.power(4).todense() #gives wrong result
结果:

matrix([[ 256,    0, 6561,    0],  #6561 isn't right
        [   0, 2401,    0,    0],
        [   0,    0,    0,    0],
        [   0,    0,    0,  625]], dtype=int32)
P = P1.copy()
#calculate ^4
for loop in range(2):
    P = P.dot(P)
P.todense()
matrix([[ 256,    0,  576,    0],
        [   0, 2401,    0,    0],
        [   0,    0,    0,    0],
        [   0,    0,    0,  625]], dtype=int32)
[[ 256    0  576    0]
 [   0 2401    0    0]
 [   0    0    0    0]
 [   0    0    0  625]]
方法2:

matrix([[ 256,    0, 6561,    0],  #6561 isn't right
        [   0, 2401,    0,    0],
        [   0,    0,    0,    0],
        [   0,    0,    0,  625]], dtype=int32)
P = P1.copy()
#calculate ^4
for loop in range(2):
    P = P.dot(P)
P.todense()
matrix([[ 256,    0,  576,    0],
        [   0, 2401,    0,    0],
        [   0,    0,    0,    0],
        [   0,    0,    0,  625]], dtype=int32)
[[ 256    0  576    0]
 [   0 2401    0    0]
 [   0    0    0    0]
 [   0    0    0  625]]
输出

matrix([[ 256,    0,  576,    0],
        [   0, 2401,    0,    0],
        [   0,    0,    0,    0],
        [   0,    0,    0,  625]], dtype=int32)
方法3

P1.dot(P1).dot(P1).dot(P1).todense()
输出:

matrix([[ 256,    0, 6561,    0],  #6561 isn't right
        [   0, 2401,    0,    0],
        [   0,    0,    0,    0],
        [   0,    0,    0,  625]], dtype=int32)
P = P1.copy()
#calculate ^4
for loop in range(2):
    P = P.dot(P)
P.todense()
matrix([[ 256,    0,  576,    0],
        [   0, 2401,    0,    0],
        [   0,    0,    0,    0],
        [   0,    0,    0,  625]], dtype=int32)
[[ 256    0  576    0]
 [   0 2401    0    0]
 [   0    0    0    0]
 [   0    0    0  625]]
方法4:

matrix([[ 256,    0, 6561,    0],  #6561 isn't right
        [   0, 2401,    0,    0],
        [   0,    0,    0,    0],
        [   0,    0,    0,  625]], dtype=int32)
P = P1.copy()
#calculate ^4
for loop in range(2):
    P = P.dot(P)
P.todense()
matrix([[ 256,    0,  576,    0],
        [   0, 2401,    0,    0],
        [   0,    0,    0,    0],
        [   0,    0,    0,  625]], dtype=int32)
[[ 256    0  576    0]
 [   0 2401    0    0]
 [   0    0    0    0]
 [   0    0    0  625]]
可以在以下位置查看结果:


关于这个主题(,)的其他线程主要关注如何进行矩阵乘法。非常感谢您的帮助。

您可以使用
**
符号:

(P1**4).todense()
结果:

matrix([[ 256,    0, 6561,    0],  #6561 isn't right
        [   0, 2401,    0,    0],
        [   0,    0,    0,    0],
        [   0,    0,    0,  625]], dtype=int32)
P = P1.copy()
#calculate ^4
for loop in range(2):
    P = P.dot(P)
P.todense()
matrix([[ 256,    0,  576,    0],
        [   0, 2401,    0,    0],
        [   0,    0,    0,    0],
        [   0,    0,    0,  625]], dtype=int32)
[[ 256    0  576    0]
 [   0 2401    0    0]
 [   0    0    0    0]
 [   0    0    0  625]]
编辑:关于
.power()
未返回预期结果的原因:

-正如他们在报告中提到的:

p.power(2)
是“元素级功率”<代码>9**4=
6561


p.power(2)
是“元素级功率”<代码>9**4=
6561
。你链接的第一个线程也这么说(甚至在标题中)。谢谢Zinki。如果你能补充一个答案,我可以接受。谢谢。恕我直言,问题不是关于如何进行乘法,而是关于为什么我得到了错误的答案。我相信有五万种乘法方法。不客气!Zinki的评论或多或少地总结了为什么你没有得到正确的结果。哦,谢谢…刚刚看到了…太棒了。如果你能修改你的答案,我会接受的。