将numpy数组转换为结构化类型

时间:2019-12-24 20:23:57

标签: numpy

我试图用下面定义的dtype(类型3)制作一个numpy数组

NB_ELEMENT1 = 2
NB_ELEMENT2 = 3
type1 = np.dtype({'names':('data1', 'data2', 'data3'),
                               'formats':((np.int8,3), (np.int8,1), (np.int8,4))})

type2 = np.dtype({'names'  :('data4','data5','datas'),
                                  'formats':((np.int8,6), (np.int8,2),(type1,NB_ELEMENT1))})

type3  = np.dtype([('data6',type2,NB_ELEMENT2)])


data_file = [ 1,2,3 ... 1,2,3]
newarray = np.array(data_file)

结构type3的大小等于数组data_file的大小

执行此操作的方法是什么?

我已经测试过

newarray.astype(type3) but it don't work

arrstr = np.array(newarray,dtype=type3)

1 个答案:

答案 0 :(得分:0)

In [107]: NB_ELEMENT1 = 2 
     ...: NB_ELEMENT2 = 3 
     ...: type1 = np.dtype({'names':('data1', 'data2', 'data3'), 
     ...:                                'formats':((np.int8,3), (np.int8,1), (n
     ...: p.int8,4))}) 
     ...:  
     ...: type2 = np.dtype({'names'  :('data4','data5','datas'), 
     ...:                                   'formats':((np.int8,6), (np.int8,2),
     ...: (type1,NB_ELEMENT1))}) 
     ...:  
     ...: type3  = np.dtype([('data6',type2,NB_ELEMENT2)])                      
/usr/local/bin/ipython3:4: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  import re

产生的dtype非常复杂:

In [108]: type3                                                                 
Out[108]: dtype([('data6', [('data4', 'i1', (6,)), ('data5', 'i1', (2,)), ('datas', [('data1', 'i1', (3,)), ('data2', 'i1'), ('data3', 'i1', (4,))], (2,))], (3,))])

In [120]: A=np.zeros(1, type3)                                                           
In [121]: A                                                                              
Out[121]: 
array([([([0, 0, 0, 0, 0, 0], [0, 0], [([0, 0, 0], 0, [0, 0, 0, 0]), ([0, 0, 0], 0, [0, 0, 0, 0])]), ([0, 0, 0, 0, 0, 0], [0, 0], [([0, 0, 0], 0, [0, 0, 0, 0]), ([0, 0, 0], 0, [0, 0, 0, 0])]), ([0, 0, 0, 0, 0, 0], [0, 0], [([0, 0, 0], 0, [0, 0, 0, 0]), ([0, 0, 0], 0, [0, 0, 0, 0])])],)],
      dtype=[('data6', [('data4', 'i1', (6,)), ('data5', 'i1', (2,)), ('datas', [('data1', 'i1', (3,)), ('data2', 'i1'), ('data3', 'i1', (4,))], (2,))], (3,))])

设置值的一种方法是提供与记录显示匹配的嵌套列表/元组。

In [122]: A['data6']                                                                     
Out[122]: 
array([[([0, 0, 0, 0, 0, 0], [0, 0], [([0, 0, 0], 0, [0, 0, 0, 0]), ([0, 0, 0], 0, [0, 0, 0, 0])]),
        ([0, 0, 0, 0, 0, 0], [0, 0], [([0, 0, 0], 0, [0, 0, 0, 0]), ([0, 0, 0], 0, [0, 0, 0, 0])]),
        ([0, 0, 0, 0, 0, 0], [0, 0], [([0, 0, 0], 0, [0, 0, 0, 0]), ([0, 0, 0], 0, [0, 0, 0, 0])])]],
      dtype=[('data4', 'i1', (6,)), ('data5', 'i1', (2,)), ('datas', [('data1', 'i1', (3,)), ('data2', 'i1'), ('data3', 'i1', (4,))], (2,))])
In [123]: _.shape                                                                        
Out[123]: (1, 3)

因此,数组具有一个字段“ data6”,其本身具有形状(3,)。在“ data4”中是(6,)或(3,6)与“ 6”组合。

In [124]: A['data6']['data4']                                                            
Out[124]: 
array([[[0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0]]], dtype=int8)
In [125]: _.shape                                                                        
Out[125]: (1, 3, 6)


In [126]: A['data6'].dtype.names                                                         
Out[126]: ('data4', 'data5', 'datas')
In [127]: A['data6']['datas']                                                            
Out[127]: 
array([[[([0, 0, 0], 0, [0, 0, 0, 0]), ([0, 0, 0], 0, [0, 0, 0, 0])],
        [([0, 0, 0], 0, [0, 0, 0, 0]), ([0, 0, 0], 0, [0, 0, 0, 0])],
        [([0, 0, 0], 0, [0, 0, 0, 0]), ([0, 0, 0], 0, [0, 0, 0, 0])]]],
      dtype=[('data1', 'i1', (3,)), ('data2', 'i1'), ('data3', 'i1', (4,))])
In [128]: A['data6']['datas'].dtype.names                                                
Out[128]: ('data1', 'data2', 'data3')
In [129]: A['data6']['datas']['data1'].shape                                             
Out[129]: (1, 3, 2, 3)

recfunctions具有将非结构化转换为结构化的功能。让我们看看它是否适用于这种复杂的东西:

In [130]: import numpy.lib.recfunctions as rf                                            

In [132]: rf.structured_to_unstructured(A)                                               
Out[132]: 
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0]], dtype=int8)
In [133]: _.shape                                                                        
Out[133]: (1, 72)

所以从72个值的数组开始:

In [138]: x = np.vstack([np.arange(24),np.arange(10,34),np.arange(20,44)])               
In [139]: rf.unstructured_to_structured(x.ravel(), type3)                                
Out[139]: 
array(([([ 0,  1,  2,  3,  4,  5], [ 6,  7], [([ 8,  9, 10], 11, [12, 13, 14, 15]), ([16, 17, 18], 19, [20, 21, 22, 23])]), 
         ([10, 11, 12, 13, 14, 15], [16, 17], [([18, 19, 20], 21, [22, 23, 24, 25]), ([26, 27, 28], 29, [30, 31, 32, 33])]), 
         ([20, 21, 22, 23, 24, 25], [26, 27], [([28, 29, 30], 31, [32, 33, 34, 35]), ([36, 37, 38], 39, [40, 41, 42, 43])])],),
      dtype=[('data6', [('data4', 'i1', (6,)), ('data5', 'i1', (2,)), ('datas', [('data1', 'i1', (3,)), ('data2', 'i1'), ('data3', 'i1', (4,))], (2,))], (3,))])