NumPy“记录数组"或“结构化阵列"或“重新排列" [英] NumPy "record array" or "structured array" or "recarray"

查看:28
本文介绍了NumPy“记录数组"或“结构化阵列"或“重新排列"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NumPy结构化数组"、记录数组"和记录数组"之间的区别(如果有)是什么?

What, if any, is the difference between a NumPy "structured array", a "record array" and a "recarray"?

NumPy 文档 暗示前两个是相同的:如果它们是,这是该对象的首选术语?

The NumPy docs imply that the first two are the same: if they are, which is the prefered term for this object?

相同的文档说(在页面底部):您可以在此处找到有关 recarray 和结构化数组的更多信息(包括两者之间的区别).这种差异有没有简单的解释?

The same documentation says (at the bottom of the page): You can find some more information on recarrays and structured arrays (including the difference between the two) here. Is there a simple explanation of this difference?

推荐答案

Records/recarrays 在

Records/recarrays are implemented in

https://github.com/numpy/numpy/blob/master/numpy/core/records.py

该文件中的一些相关引述

Some relevant quotes from this file

记录数组记录数组将结构化数组的字段公开为属性.recarray 几乎与标准数组相同(支持已命名字段)最大的区别是它可以使用属性查找来查找字段,它是使用构造的一个记录.

Record Arrays Record arrays expose the fields of structured arrays as properties. The recarray is almost identical to a standard array (which supports named fields already) The biggest difference is that it can use attribute-lookup to find the fields and it is constructed using a record.

recarrayndarray 的子类(与 matrixmasked arrays 相同).但请注意,它的构造函数与 np.array 不同.它更像是 np.empty(size, dtype).

recarray is a subclass of ndarray (in the same way that matrix and masked arrays are). But note that it's constructor is different from np.array. It is more like np.empty(size, dtype).

class recarray(ndarray):
    """Construct an ndarray that allows field access using attributes.
    This constructor can be compared to ``empty``: it creates a new record
       array but does not fill it with data.

实现唯一字段作为属性行为的关键函数是__getattribute__(__getitem__实现索引):

The key function for implementing the unique field as attribute behavior is __getattribute__ (__getitem__ implements indexing):

def __getattribute__(self, attr):
    # See if ndarray has this attr, and return it if so. (note that this
    # means a field with the same name as an ndarray attr cannot be
    # accessed by attribute).
    try:
        return object.__getattribute__(self, attr)
    except AttributeError:  # attr must be a fieldname
        pass

    # look for a field with this name
    fielddict = ndarray.__getattribute__(self, 'dtype').fields
    try:
        res = fielddict[attr][:2]
    except (TypeError, KeyError):
        raise AttributeError("recarray has no attribute %s" % attr)
    obj = self.getfield(*res)

    # At this point obj will always be a recarray, since (see
    # PyArray_GetField) the type of obj is inherited. Next, if obj.dtype is
    # non-structured, convert it to an ndarray. If obj is structured leave
    # it as a recarray, but make sure to convert to the same dtype.type (eg
    # to preserve numpy.record type if present), since nested structured
    # fields do not inherit type.
    if obj.dtype.fields:
        return obj.view(dtype=(self.dtype.type, obj.dtype.fields))
    else:
        return obj.view(ndarray)

它首先尝试获取一个常规属性——比如.shape.strides.data,以及所有的方法(.sum.reshape 等).如果失败,它将在 dtype 字段名称中查找名称.所以它实际上只是一个结构化数组,带有一些重新定义的访问方法.

It first it tries to get a regular attribute - things like .shape, .strides, .data, as well as all the methods (.sum, .reshape, etc). Failing that it then looks up the name in the dtype field names. So it is really just a structured array with some redefined access methods.

尽我所能告诉 record arrayrecarray 是一样的.

As best I can tell record array and recarray are the same.

另一个文件显示了一些历史

Another file shows something of the history

https://github.com/numpy/numpy/blob/master/numpy/lib/recfunctions.py

用于操作结构化数组的实用程序集合.其中大部分功能最初由 John Hunter 为matplotlib.为方便起见,它们已被重写和扩展.

Collection of utilities to manipulate structured arrays. Most of these functions were initially implemented by John Hunter for matplotlib. They have been rewritten and extended for convenience.

这个文件中的许多函数都以​​:

Many of the functions in this file end with:

    if asrecarray:
        output = output.view(recarray)

您可以将数组作为 recarray 视图返回这一事实表明该层是多么薄".

The fact that you can return an array as recarray view shows how 'thin' this layer is.

numpy 历史悠久,合并了几个独立的项目.我的印象是 recarray 是一个较旧的想法,结构化数组构建在通用的 dtype 上的当前实现.recarrays 似乎比任何新开发都更方便和向后兼容.但我必须研究 github 文件历史记录,以及任何最近的问题/拉取请求.

numpy has a long history, and merges several independent projects. My impression is that recarray is an older idea, and structured arrays the current implementation that built on a generalized dtype. recarrays seem to be kept for convenience and backward compatibility than any new development. But I'd have to study the github file history, and any recent issues/pull requests to be sure.

这篇关于NumPy“记录数组"或“结构化阵列"或“重新排列"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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