用另一种样式制作一个带有shape和offset参数的numpy数组 [英] make a numpy array with shape and offset argument in another style

查看:42
本文介绍了用另一种样式制作一个带有shape和offset参数的numpy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以3元素实体(3d位置)和单个元素(每个x,y,z坐标)访问数组.经过研究,我最终做了以下工作.

I wanted to access my array both as a 3-element entity (3d position) and individual element (each of x,y,z coordinate). After some researching, I ended up doing the following.

>>> import numpy as np
>>> arr = np.zeros(5, dtype={'pos': (('<f8', (3,)), 0),
                               'x': (('<f8', 1), 0),
                               'y': (('<f8', 1), 8),
                               'z': (('<f8', 1), 16)})
>>> arr["x"] = 0
>>> arr["y"] = 1
>>> arr["z"] = 2

# I can access the whole array by "pos"
>>> print(arr["pos"])
>>> array([[ 1.,  2.,  3.],
           [ 1.,  2.,  3.],
           [ 1.,  2.,  3.],
           [ 1.,  2.,  3.],
           [ 1.,  2.,  3.]])

但是,我一直以这种风格制作数组:

However, I've always been making array in this style:

>>> arr = np.zeros(10, dtype=[("pos", "f8", (3,))])

但是我找不到用这种样式同时指定元素的偏移量和形状的方法.有没有办法做到这一点?

But I can't find a way to specify both the offset and the shape of the element at the same time in this style. Is there a way to do this?

推荐答案

参考docs页面,您正在使用具有(数据类型,偏移量)

{'field1': ..., 'field2': ..., ...}

dt1 = {'pos': (('<f8', (3,)), 0),
       'x': (('<f8', 1), 0),
       'y': (('<f8', 1), 8),
       'z': (('<f8', 1), 16)}

生成的 dtype 的显示是另一种词典格式:

The display for the resulting dtype is the other dictionary format:

{'names': ..., 'formats': ..., 'offsets': ..., 'titles': ..., 'itemsize': ...}

In [15]: np.dtype(dt1)
Out[15]: dtype({'names':['x','pos','y','z'], 
                'formats':['<f8',('<f8', (3,)),'<f8','<f8'], 
                'offsets':[0,0,8,16], 'itemsize':24})

In [16]: np.dtype(dt1).fields
Out[16]: 
mappingproxy({'pos': (dtype(('<f8', (3,))), 0),
              'x': (dtype('float64'), 0),
              'y': (dtype('float64'), 8),
              'z': (dtype('float64'), 16)})

在文档页面上的其他任何地方都未提及

偏移量.

offsets aren't mentioned any where else on the documentation page.

最后一种格式是 union 类型.至于允许还是不鼓励,还不清楚.这些示例似乎不起作用.多字段索引的工作方式已经发生了一些变化,这可能会影响到这一点.

The last format is a union type. It's a little unclear as to whether that's allowed or discouraged. The examples don't seem to work. There have been some changes in how multifield indexing works, and that may have affected this.

让我们以各种方式查看数组:

Let's play around with various ways of viewing the array:

In [25]: arr
Out[25]: 
array([(0., [ 0. , 10. ,  0. ], 10., 0. ),
       (1., [ 1. , 11. ,  0.1], 11., 0.1),
       (2., [ 2. , 12. ,  0.2], 12., 0.2),
       (3., [ 3. , 13. ,  0.3], 13., 0.3),
       (4., [ 4. , 14. ,  0.4], 14., 0.4)],
      dtype={'names':['x','pos','y','z'], 'formats':['<f8',('<f8', (3,)),'<f8','<f8'], 'offsets':[0,0,8,16], 'itemsize':24})

In [29]: dt3=[('x','<f8'),('y','<f8'),('z','<f8')]
In [30]: np.dtype(dt3)
Out[30]: dtype([('x', '<f8'), ('y', '<f8'), ('z', '<f8')])
In [31]: np.dtype(dt3).fields
Out[31]: 
mappingproxy({'x': (dtype('float64'), 0),
              'y': (dtype('float64'), 8),
              'z': (dtype('float64'), 16)})
In [32]: arr.view(dt3)
Out[32]: 
array([(0., 10., 0. ), (1., 11., 0.1), (2., 12., 0.2), (3., 13., 0.3),
       (4., 14., 0.4)], dtype=[('x', '<f8'), ('y', '<f8'), ('z', '<f8')])

In [33]: arr['pos']
Out[33]: 
array([[ 0. , 10. ,  0. ],
       [ 1. , 11. ,  0.1],
       [ 2. , 12. ,  0.2],
       [ 3. , 13. ,  0.3],
       [ 4. , 14. ,  0.4]])

In [35]: arr.view('f8').reshape(5,3)
Out[35]: 
array([[ 0. , 10. ,  0. ],
       [ 1. , 11. ,  0.1],
       [ 2. , 12. ,  0.2],
       [ 3. , 13. ,  0.3],
       [ 4. , 14. ,  0.4]])

In [37]: arr.view(dt4)
Out[37]: 
array([([ 0. , 10. ,  0. ],), ([ 1. , 11. ,  0.1],),
       ([ 2. , 12. ,  0.2],), ([ 3. , 13. ,  0.3],),
       ([ 4. , 14. ,  0.4],)], dtype=[('pos', '<f8', (3,))])
In [38]: arr.view(dt4)['pos']
Out[38]: 
array([[ 0. , 10. ,  0. ],
       [ 1. , 11. ,  0.1],
       [ 2. , 12. ,  0.2],
       [ 3. , 13. ,  0.3],
       [ 4. , 14. ,  0.4]])

这篇关于用另一种样式制作一个带有shape和offset参数的numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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