Python 显示 ValueError:形状 (1,3) 和 (1,3) 未对齐:3 (dim 1) != 1 (dim 0)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39608421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Showing ValueError: shapes (1,3) and (1,3) not aligned: 3 (dim 1) != 1 (dim 0)
提问by shome
I am trying to use the following matrices and perform a dot product as shown in the code. I checked the size of the matrices and all are (3, 1) but it is throwing me error for the last two dot products.
我正在尝试使用以下矩阵并执行代码中所示的点积。我检查了矩阵的大小,所有矩阵都是 (3, 1) 但它在最后两个点积中抛出了错误。
coordinate1 = [-7.173, -2.314, 2.811]
coordinate2 = [-5.204, -3.598, 3.323]
coordinate3 = [-3.922, -3.881, 4.044]
coordinate4 = [-2.734, -3.794, 3.085]
import numpy as np
from numpy import matrix
coordinate1i=matrix(coordinate1)
coordinate2i=matrix(coordinate2)
coordinate3i=matrix(coordinate3)
coordinate4i=matrix(coordinate4)
b0 = coordinate1i - coordinate2i
b1 = coordinate3i - coordinate2i
b2 = coordinate4i - coordinate3i
n1 = np.cross(b0, b1)
n2 = np.cross(b2, b1)
n12cross = np.cross(n1,n2)
x1= np.cross(n1,b1)/np.linalg.norm(b1)
print np.shape(x1)
print np.shape(n2)
np.asarray(x1)
np.asarray(n2)
y = np.dot(x1,n2)
x = np.dot(n1,n2)
return np.degrees(np.arctan2(y, x))
采纳答案by shome
By converting the matrix to array by using
通过使用将矩阵转换为数组
n12 = np.squeeze(np.asarray(n2))
X12 = np.squeeze(np.asarray(x1))
solved the issue.
解决了这个问题。
回答by Shinto Joseph
The column of the first matrix and the row of the second matrix should be equal and the order should be like this only
第一个矩阵的列和第二个矩阵的行应该相等,顺序应该只是这样
column of first matrix = row of second matrix
and do not follow the below step
并且不要按照下面的步骤
row of first matrix = column of second matrix
it will throw an error
它会抛出一个错误
回答by Eric
Unlike standard arithmetic, which desires matching dimensions, dot products require that the dimensions are one of:
与需要匹配维度的标准算术不同,点积要求维度是以下之一:
(X..., A, B) dot (Y..., B, C) -> (X..., Y..., A, C)
, where...
means "0 or more different values(B,) dot (B, C) -> (C,)
(A, B) dot (B,) -> (A,)
(B,) dot (B,) -> ()
(X..., A, B) dot (Y..., B, C) -> (X..., Y..., A, C)
, 其中...
表示“0 个或多个不同的值(B,) dot (B, C) -> (C,)
(A, B) dot (B,) -> (A,)
(B,) dot (B,) -> ()
Your problem is that you are using np.matrix
, which is totally unnecessary in your code - the main purpose of np.matrix
is to translate a * b
into np.dot(a, b)
. As a general rule, np.matrix
is probably not a good choice.
你的问题是,你正在使用np.matrix
,这是在你的代码完全不必要的-主要目的np.matrix
是翻译a * b
成np.dot(a, b)
。一般来说,np.matrix
可能不是一个好的选择。