【问题标题】:Numpy error:"ValueError: cannot copy sequence with size 2 to array axis with dimension 4" in pythonNumpy错误:“ValueError:无法在python中将大小为2的序列复制到维度为4的数组轴”
【发布时间】:2021-10-12 21:17:03
【问题描述】:

我的代码有错误,我做了测试代码。
我的测试代码的一点描述:我导入了numpy模块。
我为起始坐标做了变量,然后做了array 7×4。
之后,我进入循环for 并遍历数组,在该数组中我从变量中执行步骤x by 10y by 5 以获取起始坐标。
然后我在数组中添加xy 中的数组和数组中的数组.
Printed array:3** 当我开始编写代码时,我就有了:

ValueError: cannot copy sequence with size 2 to array axis with dimension 4

如何解决这个错误?
这是代码:

import numpy as np

#FOR TEST

pose = (640, 154)

all_poses = np.zeros((1, 7, 4))


for i in range(0, 6):
    for j in range(0, 4):
        y = pose[1] - i * 5
        x = pose[0] - j * 10
        cortege = (x, y)
        all_poses[i, j] = cortege

print(all_poses) 

【问题讨论】:

    标签: python arrays numpy for-loop valueerror


    【解决方案1】:

    您收到错误的原因是 cortege 的尺寸是 1x2,而 all_poses[i, j] 的尺寸是 1x4。因此,当您执行all_poses[i, j] = cortege 时,您正在执行类似[0, 0, 0, 0] = [650, 154] 的操作。此处的尺寸不匹配,您会收到错误消息。

    避免错误的一种方法是将开头的 all_poses 的尺寸设置为 7x2 而不是 7x4,或者执行all_poses[i, j] = cortege*2,这会将 [650, 154, 650, 154] 添加到 all_poses[i, j] ,从而匹配尺寸。尽管您为避免错误所做的工作取决于您希望使用代码实现的目标,但您的问题并不清楚。你能解释一下你希望你的代码做什么吗?

    【讨论】:

      猜你喜欢
      • 2013-09-12
      • 2016-01-02
      • 2021-07-24
      • 2020-10-15
      • 2016-09-20
      • 2023-03-06
      • 1970-01-01
      • 2017-10-13
      • 2020-05-04
      相关资源
      最近更新 更多