3个矩阵的numpy dot积

时间:2016-03-13 15:27:17

标签: python arrays numpy linear-algebra

我希望以特定方式组合3个矩阵,我确信对于习惯使用numpy的人来说这很容易:

w = np.array([[-0.46733567,  0.38864732],
                 [-0.42436867, -1.08760098],
                 [-1.01118741,  0.99096466]])   

a = array([[ 0.63127368,  0.00167775,  0.97812284]])

d = [[-0.43252997]] # although d is length 1 in this example, its often >1 in length and code needs to be able to generalize, there will always be the same length of cols and and arrays between a and w, i.e in this example there are 3 arrays in w and 1 of length 3 in a (please excuse my terminology, I am new to linear algebra).

我正在尝试将它们组合起来找到dx,以便:

dx1 = [-0.46733567,  0.38864732] * 0.63127368 * -0.43252997
dx2 = [-0.42436867, -1.08760098] * 0.00167775 * -0.43252997
dx3 = [-1.01118741,  0.99096466] * 0.97812284 * -0.43252997

如果d是>长度为1,例如d = np.array([[ - 0.43252997],[0.87500009]])然后:

dx1_1 = [-0.46733567,  0.38864732] * 0.63127368 * -0.43252997
dx2_1 = [-0.42436867, -1.08760098] * 0.00167775 * -0.43252997
dx3_1 = [-1.01118741,  0.99096466] * 0.97812284 * -0.43252997
dx1_2 = [-0.46733567,  0.38864732] * 0.63127368 * 0.87500009
dx2_2 = [-0.42436867, -1.08760098] * 0.00167775 * 0.87500009
dx3_2 = [-1.01118741,  0.99096466] * 0.97812284 * 0.87500009

我尝试了所有这些,但似乎没有成功,除非我错过了什么?

out = np.dot(d, w) * a 
out = np.dot(d, w) * a.T 
out = np.dot(d, w.T) * a 
out = np.dot(a, w) * d
out = np.dot(a, w.T) * d

在许多情况下我收到错误:

ValueError: shapes (3,1) and (3,2) not aligned: 1 (dim 1) != 3 (dim 0)

任何有助于解决这个问题的指针都会非常感激

3 个答案:

答案 0 :(得分:3)

你不需要任何点积,因为你不想总结任何东西。

我在第三个轴上重复你的数组(如果d长于1,这就相关)。

然后乘以适当的轴......:

w = np.array([[-0.46733567,  0.38864732],
                 [-0.42436867, -1.08760098],
                 [-1.01118741,  0.99096466]])   

a = np.array([[ 0.63127368,  0.00167775,  0.97812284]])

d = np.array([[-0.43252997]])

(dim1, dim2), dim3 = w.shape, len(d[0])  

wnew = w.reshape(dim1, dim2, 1)
anew = a.reshape(dim1, 1, 1)
dnew = d.reshape(1, 1, dim3)

product = wnew * anew * dnew
i, j = 0, 0
print product[i, :, j]

最后一个语句打印出您所谓的dx_ij

答案 1 :(得分:2)

按照输入数组的格式,特别是所有输入数组的形状为ueye_api并假设您希望将输出存储在2D数组中,这是一种方法使用broadcasting -

3D

示例运行 -

(w.T)[:,:,None]*(a[0,:,None]*d[:,0])

从原始和拟议的基于广播的验证方法打印输出 -

In [48]: # Input arrays
    ...: w = np.array([[-0.46733567,  0.38864732],
    ...:                  [-0.42436867, -1.08760098],
    ...:                  [-1.01118741,  0.99096466]])   
    ...: 
    ...: a = np.array([[ 0.63127368,  0.00167775,  0.97812284]])
    ...: 
    ...: d= np.array([[-0.43252997],[0.87500009]])
    ...: 
    ...: dx1_1 = np.array([-0.46733567,  0.38864732]) * 0.63127368 * -0.43252997
    ...: dx2_1 = np.array([-0.42436867, -1.08760098]) * 0.00167775 * -0.43252997
    ...: dx3_1 = np.array([-1.01118741,  0.99096466]) * 0.97812284 * -0.43252997
    ...: dx1_2 = np.array([-0.46733567,  0.38864732]) * 0.63127368 * 0.87500009
    ...: dx2_2 = np.array([-0.42436867, -1.08760098]) * 0.00167775 * 0.87500009
    ...: dx3_2 = np.array([-1.01118741,  0.99096466]) * 0.97812284 * 0.87500009
    ...: 
    ...: # Let's store these in a 3D array
    ...: p1 = np.column_stack((dx1_1,dx2_1,dx3_1))
    ...: p2 = np.column_stack((dx1_2,dx2_2,dx3_2))
    ...: out = np.dstack((p1,p2))
    ...: 

如果输入数组In [49]: out # Output from original approach Out[49]: array([[[ 1.27603568e-01, -2.58139646e-01], [ 3.07954650e-04, -6.22986533e-04], [ 4.27800472e-01, -8.65432403e-01]], [[ -1.06118124e-01, 2.14674993e-01], [ 7.89247187e-04, -1.59663239e-03], [ -4.19244884e-01, 8.48124609e-01]]]) In [50]: (w.T)[:,:,None]*(a[0,:,None]*d[:,0]) # Output from proposed solution Out[50]: array([[[ 1.27603568e-01, -2.58139646e-01], [ 3.07954650e-04, -6.22986533e-04], [ 4.27800472e-01, -8.65432403e-01]], [[ -1.06118124e-01, 2.14674993e-01], [ 7.89247187e-04, -1.59663239e-03], [ -4.19244884e-01, 8.48124609e-01]]]) ad数组:

1D

,解决方案将简化为 -

a = np.array([ 0.63127368,  0.00167775,  0.97812284])
d = np.array([-0.43252997,0.87500009])

答案 2 :(得分:1)

当你有两个矩阵时,它们应该有一个共享的维度。 在您的示例中:

>>> out = np.dot(a, w)
>>> print out
[[-1.28479419  1.21280327]]

了解更多信息:https://en.wikipedia.org/wiki/Matrix_multiplication 如果A是n×m矩阵而B是m×p矩阵,则矩阵乘积AB(没有乘符号或点表示)被定义为n×p矩阵。

最诚挚的问候, 亚龙