Numpy数据类型会导致数组重复



我想使用用户定义的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]],)]]

编辑:打印两个阶段的数据类型:

{'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}
In [4]: poly = np.random.rand (3,2)                                                            
In [5]: dt1 = np.dtype({'names':['dummy','poly'], 'formats':['<f8',('<f8', (3, 2))], 'offsets':
...: [0,8], 'itemsize':56, 'aligned':True})                                                                                        
In [6]: dt2 = np.dtype({'names':['poly'], 'formats':[('<f8', (3, 2))], 'offsets':[0], 'itemsize
...: ':48, 'aligned':True})                                                                 
In [7]: dt1                                                                                    
Out[7]: dtype([('dummy', '<f8'), ('poly', '<f8', (3, 2))], align=True)

制作第一个阵列:

In [8]: np.array((0,poly), dtype=dt1)                                                          
Out[8]: 
array((0., [[0.06466034, 0.43310972], [0.58102027, 0.53106307], [0.23957058, 0.26556208]]),
dtype={'names':['dummy','poly'], 'formats':['<f8',('<f8', (3, 2))], 'offsets':[0,8], 'itemsize':56, 'aligned':True})

第二个数据类型有1个字段;即便如此,我们仍然需要将数据提供为元组或元组列表:

In [9]: dt2                                                                                    
Out[9]: dtype([('poly', '<f8', (3, 2))], align=True)
In [10]: np.array((poly,), dt2)                                                                
Out[10]: 
array(([[0.06466034, 0.43310972], [0.58102027, 0.53106307], [0.23957058, 0.26556208]],),
dtype={'names':['poly'], 'formats':[('<f8', (3, 2))], 'offsets':[0], 'itemsize':48, 'aligned':True})

最新更新