【发布时间】:2020-06-23 16:15:03
【问题描述】:
我想使用用户定义的 numba 数据结构创建一个 numpy 数组。当我在数据结构中包含一个虚拟变量时,一切正常,但是当我删除它时,生成的矩阵是我想要的数据的重复。但我不知道为什么 numpy 会重复我的数据以及如何避免它。
import numpy as np
from numba.types import float64, Record, NestedArray
poly = np.random.rand (3,2)
args_dtype = Record.make_c_struct([
('dummy', float64),
('poly', NestedArray(dtype=float64, shape=poly.shape)),])
args = np.array((0,poly), dtype=args_dtype)
print(args)
print('-------------------------')
args_dtype = Record.make_c_struct([
('poly', NestedArray(dtype=float64, shape=poly.shape)),])
args = np.array(poly, dtype=args_dtype)
print(args)
输出:
(0., [[0.72543644, 0.77155485], [0.08560247, 0.11165251], [0.48421994, 0.15144579]])
-------------------------
[[([[0.72543644, 0.72543644], [0.72543644, 0.72543644], [0.72543644, 0.72543644]],)
([[0.77155485, 0.77155485], [0.77155485, 0.77155485], [0.77155485, 0.77155485]],)]
[([[0.08560247, 0.08560247], [0.08560247, 0.08560247], [0.08560247, 0.08560247]],)
([[0.11165251, 0.11165251], [0.11165251, 0.11165251], [0.11165251, 0.11165251]],)]
[([[0.48421994, 0.48421994], [0.48421994, 0.48421994], [0.48421994, 0.48421994]],)
([[0.15144579, 0.15144579], [0.15144579, 0.15144579], [0.15144579, 0.15144579]],)]]
编辑:为两个阶段打印 dtype:
{'names':['dummy','poly'], 'formats':['<f8',('<f8', (3, 2))], 'offsets':[0,8], 'itemsize':56, 'aligned':True}
-------------------------
{'names':['poly'], 'formats':[('<f8', (3, 2))], 'offsets':[0], 'itemsize':48, 'aligned':True}
【问题讨论】:
-
打印两个阶段的
dtype。我不知道numba在做什么,但知道在尝试与结构化数组进行转换时会出现这样的重复。numpy.lib.recfunctions有一对函数可以正确处理这个问题。 -
@hpaulj dtype 已添加到问题中。
标签: python numpy types casting numba