形状(3,1)和(3,)未对齐:1(尺寸1)!=3(尺寸0)

2024-09-30 14:38:55 发布

您现在位置:Python中文网/ 问答频道 /正文

这里定义了一个函数,因此它必须是一个标量,但当调用该函数时,我会得到以下错误:形状(3,1)和(3,)未对齐:1(尺寸1)!=3(尺寸0)

def Angle_sun(panel):
  print(panel)
  sun = np.array([[0], [-1368], [0]])
  dott = np.dot(panel, sun)
  return math.acos(dott/(np.linalg.norm(panel)) * np.linalg.norm(sun))

其中面板=[[0.],[0.92],[0.39]]

面板为a(3,1)


Tags: 函数面板norm定义尺寸def错误np
1条回答
网友
1楼 · 发布于 2024-09-30 14:38:55

请查看matrix dot product (Wikipedia),对于a.b,所需矩阵的大小应分别为NxMMxN。如果您查看^{},您将知道为传递给它的不同形状的矩阵生成了什么输出

numpy.dot(a, b, out=None)
Dot product of two arrays. Specifically, If both a and b are 1-D arrays, it is inner product of vectors (without complex conjugation).

If both a and b are 2-D arrays, it is matrix multiplication, but using matmul or a @ b is preferred.

If either a or b is 0-D (scalar), it is equivalent to multiply and using numpy.multiply(a, b) or a * b is preferred.

If a is an N-D array and b is a 1-D array, it is a sum product over the last axis of a and b.

If a is an N-D array and b is an M-D array (where M>=2), it is a sum product over the last axis of a and the second-to-last axis of b

由于上面的sun矩阵的维数为3x1,并且您正在执行panel.sun,因此panel矩阵的维数应为1x3,那么两个形状为1x3的矩阵的点积为3x1

因此,您拥有的面板列表应该是1D列表,即[0.0, 0.92, 0.39],而不是[[0.0], [0.92], [0.39]],这当然是一个2D列表

相关问题 更多 >