我有一个两个numpy 1-D阵列: a 和 b 。
a 的形状(10,) b 具有形状(16,)。
我想让 a 具有形状(10,1)和 b 以使形状(16,1)与矩阵相乘,如下所示:np。 matmul(a,bT)。
如果成功,则应该产生(10,16)2-D阵列。 有谁知道怎么做?
答案 0 :(得分:0)
IIUC:
In [47]: a = np.arange(10)
In [48]: b = np.arange(16)
In [49]: a[:,None] * b
Out[49]:
array([[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
[ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30],
[ 0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45],
[ 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60],
[ 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75],
[ 0, 6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72, 78, 84, 90],
[ 0, 7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84, 91, 98, 105],
[ 0, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96, 104, 112, 120],
[ 0, 9, 18, 27, 36, 45, 54, 63, 72, 81, 90, 99, 108, 117, 126, 135]])
答案 1 :(得分:0)
您需要明确使用reshape
。 b.T
和b.transpose()
对1D数组没有任何意义:
a = np.arange(10)
b = np.arange(16)
c = a.reshape(10, 1) @ b.reshape(1, 16)
正如预期的那样, c.shape
现在为(10, 16)
。
答案 2 :(得分:0)
您可以使用reshape方法。
import numpy as np
a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]).reshape((10, 1))
b = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]).reshape((16, 1))
c = np.matmul(a, b.T)
print(c.shape)