NumPy“结构化阵列”,“记录阵列”和“重新阵列”之间有什么区别?NumPy“记录阵列”或“结构化阵列”或“重新阵列”
NumPy docs意味着前两个是相同的:如果它们是,哪个是这个对象的首选项?
相同的文档说(在页面的底部): 你可以找到一些关于recarrays和结构化阵列的更多信息(包括两者之间的差异)here。有没有简单的解释这种差异?
NumPy“结构化阵列”,“记录阵列”和“重新阵列”之间有什么区别?NumPy“记录阵列”或“结构化阵列”或“重新阵列”
NumPy docs意味着前两个是相同的:如果它们是,哪个是这个对象的首选项?
相同的文档说(在页面的底部): 你可以找到一些关于recarrays和结构化阵列的更多信息(包括两者之间的差异)here。有没有简单的解释这种差异?
记录/ recarrays在
https://github.com/numpy/numpy/blob/master/numpy/core/records.py
一些相关报价执行此文件
记录阵列 记录阵列暴露结构数组作为属性的领域。 重新阵列与标准阵列(已支持 命名字段)几乎完全相同。最大的区别是它可以使用 属性查找来查找字段,并且它使用 记录构建。
recarray
是ndarray
一个子类(以同样的方式,matrix
和masked arrays
是)。但请注意,它的构造函数与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
是一个较老的想法,并且结构化数组是当前实现的基础,其基于广义的dtype
。为了方便和向后兼容,似乎保留了任何新的开发。但我不得不研究github
文件历史记录,以及任何最近的问题/拉取请求。
[结构化阵列(又名“记录阵列”)](http://docs.scipy.org/doc/numpy/user/basics.rec.html) –
我已经澄清了这个问题,@Ashwini Chaudhary - 谢谢。 – xnx
关于文档差异的解释有什么不清楚? recarray支持访问'arr.foo'形式的字段,而普通结构化数组只支持'arr ['foo']'格式的访问,但查找速度更快。我永远不会称之为“结构化数组”,“记录数组”,正是因为它引起了如此多的潜在“混淆”困惑。 – zehnpaard