numpy:要加入结构化数组吗?

编程入门 行业动态 更新时间:2024-10-27 10:24:08
本文介绍了numpy:要加入结构化数组吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在类似这样的列表中有很多 numpy结构化数组这个例子:

I have many numpy structured arrays in a list like this example:

import numpy a1 = numpy.array([(1, 2), (3, 4), (5, 6)], dtype=[('x', int), ('y', int)]) a2 = numpy.array([(7,10), (8,11), (9,12)], dtype=[('z', int), ('w', float)]) arrays = [a1, a2]

所需的输出

将它们连接在一起以创建一个统一的结构化数组的正确方法是什么?

Desired Output

What is the correct way to join them all together to create a unified structured array like the following?

desired_result = numpy.array([(1, 2, 7, 10), (3, 4, 8, 11), (5, 6, 9, 12)], dtype=[('x', int), ('y', int), ('z', int), ('w', float)])

当前方法

这是我目前正在使用的方法,但是它很慢,因此我怀疑必须有一种更有效的方法.

Current Approach

This is what I'm currently using, but it is very slow, so I suspect there must be a more efficent way.

from numpy.lib.recfunctions import append_fields def join_struct_arrays(arrays): for array in arrays: try: result = append_fields(result, array.dtype.names, [array[name] for name in array.dtype.names], usemask=False) except NameError: result = array return result

推荐答案

这是一个应该更快的实现.它将所有内容转换为numpy.uint8的数组,并且不使用任何临时对象.

Here is an implementation that should be faster. It converts everything to arrays of numpy.uint8 and does not use any temporaries.

def join_struct_arrays(arrays): sizes = numpy.array([a.itemsize for a in arrays]) offsets = numpy.r_[0, sizes.cumsum()] n = len(arrays[0]) joint = numpy.empty((n, offsets[-1]), dtype=numpy.uint8) for a, size, offset in zip(arrays, sizes, offsets): joint[:,offset:offset+size] = a.view(numpy.uint8).reshape(n,size) dtype = sum((a.dtype.descr for a in arrays), []) return joint.ravel().view(dtype)

编辑:简化了代码,避免了不必要的as_strided().

Edit: Simplified the code and avoided the unnecessary as_strided().

更多推荐

numpy:要加入结构化数组吗?

本文发布于:2023-10-19 20:30:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1508690.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   结构化   numpy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!