栏目分类:
子分类:
返回
文库吧用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
文库吧 > IT > 面试经验 > 面试问答

NumPy“记录数组”或“结构化数组”或“ recarray”

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

NumPy“记录数组”或“结构化数组”或“ recarray”

记录/记录数组在

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

此文件中的一些相关引号

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

recarray
ndarray
matrix
和和
maskedarrays
是)相同的子类。但请注意,它的构造函数不同于
np.array
。更像是
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__
实现索引):

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
字段名称中查找名称。因此,它实际上只是带有一些重新定义的访问方法的结构化数组。

尽我所能告诉,

record array
并且
recarray
是相同的。

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

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

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

该文件中的许多功能都以:

    if asrecarray:        output = output.view(recarray)

您可以将数组作为

recarray
视图返回,这一事实表明了该层的“厚度”。

numpy
历史悠久,并合并了几个独立的项目。我的印象是,这
recarray
是一个较旧的主意,结构化数组是基于generalized的当前实现的
dtype

recarrays
似乎为了方便和向后兼容而保留了比任何新开发的产品。但是我必须研究
github
文件历史记录以及任何最近出现的问题/请求才能确定。



转载请注明:文章转载自 www.wk8.com.cn
本文地址:https://www.wk8.com.cn/it/640254.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 wk8.com.cn

ICP备案号:晋ICP备2021003244-6号