【问题标题】:Adding new lines results in ValueError in numpy.asarray在 numpy.asarray 中添加新行会导致 ValueError
【发布时间】:2019-09-22 21:26:47
【问题描述】:

在添加邻居语句(我用“新”评论)之前,一切正常。现在,当使用 numpy.asarray 时,出现以下错误:

ValueError: 无法将输入数组从形状 (3,3) 广播到形状 (3)。

我真的很困惑,因为新行并没有改变旋转数组的任何内容。

def rre(mesh, rotations):
"""
Relative Rotation Encoding (RRE).
Return a compact representation of all relative face rotations.
"""
all_rel_rotations = neighbors = []
for f in mesh.faces():
    temp = [] # new
    for n in mesh.ff(f):
        rel_rotation = np.matmul(rotations[f.idx()], np.linalg.inv(rotations[n.idx()]))
        all_rel_rotations.append(rel_rotation)
        temp.append(n.idx()) # new
    neighbors.append(temp) # new
all_rel_rotations = np.asarray(all_rel_rotations)
neighbors = np.asarray(neighbors) # new
return all_rel_rotations, neighbors

【问题讨论】:

标签: python numpy valueerror openmesh


【解决方案1】:

问题的根源很可能是这行:

all_rel_rotations = neighbors = []

在 Python 中,列表是可变的,all_rel_rotationsneighbors 指向同一个列表,所以如果你这样做 all_rel_rotations.append(42) 你会看到 neighbors = [42, ]

行:

all_rel_rotations.append(rel_rotation)

附加一个二维数组,而

neighbors.append(temp)

将一维数组(或其他方式)附加到同一个列表。那么:

all_rel_rotations = np.asarray(all_rel_rotations)

尝试转换为数组并感到困惑。

如果你需要列出做

all_rel_rotations = []
neighbors = []

【讨论】:

  • 这是每个从 python 开始的人都会犯的错误:必须了解可变变量和非可变变量之间的区别以及它们是如何处理的:D.
  • 好吧,至少这个特别的事情不会再发生了。您可能为我节省了数小时的搜索和挫败感:)
  • 为了以后节省时间我建议尽快学会使用python调试器
猜你喜欢
  • 1970-01-01
  • 2015-04-04
  • 2011-07-05
  • 1970-01-01
  • 1970-01-01
  • 2014-12-22
  • 2016-10-28
  • 1970-01-01
  • 2012-10-13
相关资源
最近更新 更多