如何获取结构化数组选择的副本 [英] How to get copy of structured array selection

查看:123
本文介绍了如何获取结构化数组选择的副本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a structured array with mixed types:

dt = np.dtype([('x', np.float64), ('y', np.float64), ('n', np.uint32)])
arr = np.empty(10, dtype=dt)

从numpy 1.16开始,如果我查看xy,则会得到一个视图:

As of numpy 1.16 or so, if I view x and y, I get a view:

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

这是一个问题,因为我希望能够将子集sub转换为xy字段的(10, 2)视图.

This is a problem because I would like to be able to convert the subset sub into a (10, 2) view of the x and y fields.

我不能只使用sub.view(dtype=np.float64).这引发了错误

I can not just use sub.view(dtype=np.float64). That raises the error

ValueError: When changing to a smaller dtype, its size must be a divisor of the size of original dtype

我可以使用np.lib.stride_tricks.as_strided,但这很麻烦而且有问题,因为它仅在我需要两个字段(或任意数量的均匀间隔的字段)时才起作用:

I can use np.lib.stride_tricks.as_strided, but that is hacky and problematic because it only works when I want two fields (or alternatively any number of evenly-spaced fields):

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

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

If sub were a copy, then I could simply view it as a (10, 2) array of floats. How can I view the selected fields as such an array, either by copying the selection or any other means?

推荐答案

repack_fields以及多字段视图中的更改:

repack_fields goes along with the change in multifield view:

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

它是副本,而不是视图.

It is a copy, not a view.

对于(n,2)个副本:

And for a (n,2) copy:

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

这篇关于如何获取结构化数组选择的副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆