Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.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 如何获取结构化数组选择的副本_Python_Numpy_Structured Array - Fatal编程技术网

Python 如何获取结构化数组选择的副本

Python 如何获取结构化数组选择的副本,python,numpy,structured-array,Python,Numpy,Structured Array,我有一个混合类型的结构化数组: dt = np.dtype([('x', np.float64), ('y', np.float64), ('n', np.uint32)]) arr = np.empty(10, dtype=dt) 从numpy 1.16左右开始,如果我查看x和y,我会看到: >>> sub = arr[['x', 'y']] >>> sub array([(6.23042070e-307, 4.67296746e-307),

我有一个混合类型的结构化数组:

dt = np.dtype([('x', np.float64), ('y', np.float64), ('n', np.uint32)])
arr = np.empty(10, dtype=dt)
从numpy 1.16左右开始,如果我查看
x
y
,我会看到:

>>> sub = arr[['x', 'y']]
>>> sub
array([(6.23042070e-307, 4.67296746e-307),
       (1.15710088e-306, 1.60221615e-306),
       (1.95821574e-306, 6.23062102e-307),
       (1.78019082e-306, 1.37959740e-306),
       (1.37959129e-306, 1.33511562e-306),
       (1.33511018e-306, 1.33511969e-306),
       (1.11261027e-306, 1.11261502e-306),
       (8.45593934e-307, 9.34600963e-307),
       (6.23038336e-307, 1.29061142e-306),
       (2.22522596e-306, 2.22522596e-306)],
      dtype={'names':['x','y'], 'formats':['<f8','<f8'], 'offsets':[0,8], 'itemsize':20})
我可以使用
np.lib.stride\u技巧。as\u strided
,但这是有问题的,因为它只在需要两个字段(或者任意数量的等距字段)时起作用:


如果
sub
是一个副本,那么我可以简单地将其视为一个
(10,2)
浮点数组。如何通过复制选择或任何其他方式将所选字段视为数组?

重新打包字段
与多字段视图中的更改一起进行:

In [135]: dt = np.dtype([('x', np.float64), ('y', np.float64), ('n', np.uint32)]) 
     ...: arr = np.empty(3, dtype=dt)                                                                  
In [136]: sub = arr[['x','y']]                                                                         
In [137]: import numpy.lib.recfunctions as rf                                                          
In [138]: rf.repack_fields(sub)                                                                        
Out[138]: 
array([(4.04359530e-316, 4.04349886e-316),
       (0.00000000e+000, 0.00000000e+000),
       (4.04355735e-316, 0.00000000e+000)],
      dtype=[('x', '<f8'), ('y', '<f8')])
In [139]: sub                                                                                          
Out[139]: 
array([(4.04359530e-316, 4.04349886e-316),
       (0.00000000e+000, 0.00000000e+000),
       (4.04355735e-316, 0.00000000e+000)],
      dtype={'names':['x','y'], 'formats':['<f8','<f8'], 'offsets':[0,8], 'itemsize':20})

structured\u to\u unstructured
正是我想要的。我还没有真正使用过Recarray。在多字段索引中有一个很大的变化,几个版本都回来了-它又关闭了-又打开了,直到他们在
recfunctions
中使用了几个新函数来稳定它。为了跟上进度,我不得不研究发行说明。有什么理由投反对票吗?
>>> shape = sub.shape + (2,)
>>> strides = (sub.dtype.itemsize,
           np.diff([x[1] for x in sub.dtype.fields.values()]).item())
>>> np.lib.stride_tricks.as_strided(sub, shape=shape, strides=strides)['x']
array([[6.23042070e-307, 4.67296746e-307],
       [1.15710088e-306, 1.60221615e-306],
       [1.95821574e-306, 6.23062102e-307],
       [1.78019082e-306, 1.37959740e-306],
       [1.37959129e-306, 1.33511562e-306],
       [1.33511018e-306, 1.33511969e-306],
       [1.11261027e-306, 1.11261502e-306],
       [8.45593934e-307, 9.34600963e-307],
       [6.23038336e-307, 1.29061142e-306],
       [2.22522596e-306, 2.22522596e-306]])
In [135]: dt = np.dtype([('x', np.float64), ('y', np.float64), ('n', np.uint32)]) 
     ...: arr = np.empty(3, dtype=dt)                                                                  
In [136]: sub = arr[['x','y']]                                                                         
In [137]: import numpy.lib.recfunctions as rf                                                          
In [138]: rf.repack_fields(sub)                                                                        
Out[138]: 
array([(4.04359530e-316, 4.04349886e-316),
       (0.00000000e+000, 0.00000000e+000),
       (4.04355735e-316, 0.00000000e+000)],
      dtype=[('x', '<f8'), ('y', '<f8')])
In [139]: sub                                                                                          
Out[139]: 
array([(4.04359530e-316, 4.04349886e-316),
       (0.00000000e+000, 0.00000000e+000),
       (4.04355735e-316, 0.00000000e+000)],
      dtype={'names':['x','y'], 'formats':['<f8','<f8'], 'offsets':[0,8], 'itemsize':20})
In [140]: rf.structured_to_unstructured(sub)                                                           
Out[140]: 
array([[4.04359530e-316, 4.04349886e-316],
       [0.00000000e+000, 0.00000000e+000],
       [4.04355735e-316, 0.00000000e+000]])
In [141]: rf.structured_to_unstructured(rf.repack_fields(sub))                                         
Out[141]: 
array([[4.04359530e-316, 4.04349886e-316],
       [0.00000000e+000, 0.00000000e+000],
       [4.04355735e-316, 0.00000000e+000]])