numpy矩阵相乘@的用法

numpy矩阵相乘matmul可以用@来代替

在正常的python编程里面@是作为修饰符使用的,但是在numpy的矩阵乘法中可以使用@来替代matmul

matmul
‘@’ operator as method with out parameter.
numpy的文档

正确使用测试代码
import numpy as np

#numpy里面的用法
a = np.array([[1, 0], [0, 1]])
b = np.array([[4, 1], [2, 2]])

np.matmul(a, b)
a @ b
运行结果如下
array([[4, 1],
       [2, 2]])
array([[4, 1],
       [2, 2]])
错误使用测试代码
#常规python编程的用法
a = [[1, 0], [0, 1]]
b = [[4, 1], [2, 2]]

np.matmul(a, b)
#会报错
a @ b
运行结果如下
array([[4, 1],
       [2, 2]])
TypeError: unsupported operand type(s) for @: 'list' and 'list'

到目前为止,我只在numpy的矩阵相乘中看到@的矩阵乘法的用法

猜你喜欢

转载自blog.csdn.net/dengyibing/article/details/78658415