gpt4 book ai didi

python:在numpy中将两个一维矩阵相乘

转载 作者:行者123 更新时间:2023-11-28 20:39:59 25 4
gpt4 key购买 nike

a = np.asarray([1,2,3])

b = np.asarray([2,3,4,5])

a.shape

(3,)

b.shape

(4,)

我想要一个 3 x 4 矩阵,它是 a 和 b 的乘积

1
2 * 2 3 4 5
3

np.dot(a, b.transpose())

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: objects are not aligned

点积仅当数组为二维时才等价于矩阵乘法,所以 np.dot 不起作用。

最佳答案

这是 np.outer(a, b) :

In [2]: np.outer([1, 2, 3], [2, 3, 4, 5])
Out[2]:
array([[ 2, 3, 4, 5],
[ 4, 6, 8, 10],
[ 6, 9, 12, 15]])

关于python:在numpy中将两个一维矩阵相乘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36871649/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com