麻木错误?如何对齐 numpy 记录数组(“recarray”)?

Nei*_*l G 2 python numpy memory-alignment

更新:numpy 错误


不幸的是以下情况:

import numpy as np
a = np.zeros(4, dtype=np.dtype([('t', '<f8'), ('d', [('a', '<i4'), ('b', '<f8')], (100,))], align=True))
b = np.require(a, requirements=['ALIGNED'])
print([x.flags['ALIGNED'] for x in [a, b]])
Run Code Online (Sandbox Code Playgroud)

印刷[False, False]

我该如何对齐a

Cha*_*ris 5

您可以在数据类型中指定对齐要求。

In [9]: a = np.zeros(4, dtype= np.dtype([('x', '<f8'), ('y', '<i4')], align=False))

In [10]: a.data
Out[10]: <read-write buffer for 0x2f94440, size 48, offset 0 at 0x2f8caf0>

In [11]: a = np.zeros(4, dtype=np.dtype([('x', '<f8'), ('y', '<i4')], align=True))
In [12]: a.data
Out[12]: <read-write buffer for 0x2f94030, size 64, offset 0 at 0x2f8c5b0>
Run Code Online (Sandbox Code Playgroud)

注意大小的差异。对于结构化类型,对齐标志在以前版本的 Numpy 中具有误导性,现在字符串和结构化类型的要求是 16 个字节,以便在 SPARC 上正常工作。Julian Taylor 在http://article.gmane.org/gmane.comp.python.numeric.general/59123给出了更详细的解释