Constructing np.array with overlapping fields in dtype

不问归期 提交于 2019-12-13 16:53:31

问题


I have a dtype as follows:

pose_dtype = np.dtype([('x', np.float64), ('y', np.float64), ('theta', np.float64)])

Right now, I can write:

pose = np.array((1, 2, np.pi), dtype=pose_dtype)

I'd like to add an xy field to make this easier to work with. I can do this with:

pose_dtype = np.dtype(dict(
    names=['x', 'y', 'theta', 'xy'],
    formats=[np.float64, np.float64, np.float64, (np.float64, 2)],
    offsets=[0, 8, 16, 0]
))

However, now I can no longer construct the array using my previous method, and have to resort to:

pose = np.array((1, 2, np.pi, [1, 2]), dtype=pose_dtype)

Which is dangerously repetitious.

Is there any way I can mark the properties as aliases of one another, so that I don't have to deal with this?


回答1:


Experiments in filling an array by field rather than by record

In [207]: pose_dtype = np.dtype(dict(
    names=['x', 'y', 'theta', 'xy'],
    formats=[np.float64, np.float64, np.float64, (np.float64, 2)],
    offsets=[0, 8, 16, 0]
))

In [209]: A=np.zeros((3,),dtype=pose_dtype)
In [210]: A
Out[210]: 
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])], 
      dtype={'names':['x','y','theta','xy'], 'formats':['<f8','<f8','<f8',('<f8', (2,))], 'offsets':[0,8,16,0], 'itemsize':24})
In [211]: A['x']=[1,2,3]
In [212]: A
Out[212]: 
array([(1.0, 0.0, 0.0, [1.0, 0.0]), (2.0, 0.0, 0.0, [2.0, 0.0]),
       (3.0, 0.0, 0.0, [3.0, 0.0])], 
      dtype={'names':['x','y','theta','xy'], 'formats':['<f8','<f8','<f8',('<f8', (2,))], 'offsets':[0,8,16,0], 'itemsize':24})
In [213]: A['y']=[4,5,6]
In [214]: A
Out[214]: 
array([(1.0, 4.0, 0.0, [1.0, 4.0]), (2.0, 5.0, 0.0, [2.0, 5.0]),
       (3.0, 6.0, 0.0, [3.0, 6.0])], 
      dtype={'names':['x','y','theta','xy'], 'formats':['<f8','<f8','<f8',('<f8', (2,))], 'offsets':[0,8,16,0], 'itemsize':24})
In [215]: A['xy']
Out[215]: 
array([[ 1.,  4.],
       [ 2.,  5.],
       [ 3.,  6.]])
In [216]: A['xy']=np.arange(10,16).reshape(3,2)
In [217]: A
Out[217]: 
array([(10.0, 11.0, 0.0, [10.0, 11.0]), (12.0, 13.0, 0.0, [12.0, 13.0]),
       (14.0, 15.0, 0.0, [14.0, 15.0])], 
      dtype={'names':['x','y','theta','xy'], 'formats':['<f8','<f8','<f8',('<f8', (2,))], 'offsets':[0,8,16,0], 'itemsize':24})

In [219]: A['xy'].dot(A['xy'].T)
Out[219]: 
array([[ 221.,  263.,  305.],
       [ 263.,  313.,  363.],
       [ 305.,  363.,  421.]])

another way of getting the 2 fields as a float array (not pretty)

In [228]: A[['x','y']].view(float).reshape(-1,2)
Out[228]: 
array([[ 10.,  11.],
       [ 12.,  13.],
       [ 14.,  15.]])


来源:https://stackoverflow.com/questions/36432067/constructing-np-array-with-overlapping-fields-in-dtype

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!