简体   繁体   中英

Numpy datatype causes repetition of the array

I want to create a numpy array using a user-defined numba data structure. When I include a dummy variable in the data structure everything works fine, but when I remove it the resulting matrix is a repetition of the data that I want. But I don't know why numpy repeats my data and how can I avoid it.

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)

The output:

(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]],)]]

Edit: printing dtype for both stages:

{'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)

Make the first array:

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})

The second dtype has 1 field; even so we still need to provide the data as a tuple or list of tuples:

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})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM