填充结构化数组会引发 "ndarray is not C-contiguous" 错误
populating a structured array throws "ndarray is not C-contiguous" error
我正在使用以这种方式定义的结构化数组:
scores = np.empty((num_of_imgs, 4),
dtype=[('id', 'u4'), ('bestT', 'u8'), ('bestR', 'f8'), ('bestP', 'f8')])
然后在 for 循环中填充它:
scores[i] = [id, bestT, bestR, bestP]
其中列表中的所有变量都是形状为 (1,) 的 numpy 数组。
然而,这行代码抛出上述错误。为什么?
你的 scores
作业正在制作一个 nx4 的 4 元组数组,我认为这是一个比你想要的更大的额外维度。应该是
scores = np.empty(num_of_imgs,
dtype=[('id', 'u4'), ('bestT', 'u8'), ('bestR', 'f8'), ('bestP', 'f8')])
然后你试图将一个列表分配给一个元组,这会抛出你的 c-contiguous 错误(numpy 在转换结构化数组的类型方面不如它对 ndarrays 有用) .使分配成为一个元组。 (使用 ()
而不是 []
)
scores[i] = (id, bestT, bestR, bestP)
我正在使用以这种方式定义的结构化数组:
scores = np.empty((num_of_imgs, 4),
dtype=[('id', 'u4'), ('bestT', 'u8'), ('bestR', 'f8'), ('bestP', 'f8')])
然后在 for 循环中填充它:
scores[i] = [id, bestT, bestR, bestP]
其中列表中的所有变量都是形状为 (1,) 的 numpy 数组。 然而,这行代码抛出上述错误。为什么?
你的
scores
作业正在制作一个 nx4 的 4 元组数组,我认为这是一个比你想要的更大的额外维度。应该是scores = np.empty(num_of_imgs, dtype=[('id', 'u4'), ('bestT', 'u8'), ('bestR', 'f8'), ('bestP', 'f8')])
然后你试图将一个列表分配给一个元组,这会抛出你的 c-contiguous 错误(numpy 在转换结构化数组的类型方面不如它对 ndarrays 有用) .使分配成为一个元组。 (使用
()
而不是[]
)scores[i] = (id, bestT, bestR, bestP)