Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Numpy数据类型导致数组重复_Python_Numpy_Types_Casting_Numba - Fatal编程技术网

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

Python Numpy数据类型导致数组重复,python,numpy,types,casting,numba,Python,Numpy,Types,Casting,Numba,我想使用用户定义的NUMBY数据结构创建一个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([

我想使用用户定义的NUMBY数据结构创建一个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}
{'names':['dummy','poly'],'formats':['
在[4]:poly=np.random.rand(3,2)

在[5]中:dt1=np.dtype({'names':['dummy','poly'],'formats':['打印两个阶段的
dtype
。我不知道
numba
在做什么,但我知道在尝试转换到结构化数组或从结构化数组转换时会出现这样的重复。
numpy.lib.recfunctions
有一对函数可以正确处理此问题。@hpaulj dtype被添加到问题中。
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})
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})