一个简单的矩阵乘法会抛出错误"shapes (2,3) and (2,3) not aligned: 3 (dim 1) != 2 (dim 0)"?

A simple matrix multiplication throws an error "shapes (2,3) and (2,3) not aligned: 3 (dim 1) != 2 (dim 0)"?

我正在尝试将两个矩阵相乘,这给了我一个错误 "shapes (2,3) and (2,3) not aligned: 3 (dim 1) != 2 (dim 0)"。代码如下。请让我知道我做错了什么?

 from numpy import *
    arr=array([[1,32,3],[2,4,6]])
    arr1=array([[1,2,39],[2,41,6]])
    m=matrix(arr)
    m1=matrix(arr1)
    print(m)
    print(m1)
    mat=m1*m;

如果您尝试进行逐元素乘法(即 1*1、32*2、3*39、2*2、4*41、6*6),那么您需要使用numpy.multiply.

import numpy as np
a = np.matrix([[1,32,3],[2,4,6]])
b = np.matrix([[1,2,39],[2,41,6]])
np.multiply(a,b)

哪个returns这个

matrix([[  1,  64, 117],
        [  4, 164,  36]])

如果你真的想做矩阵乘法(线性代数),那么请看上面其他人的评论。