我试图实现numpy recarray(recsub)的一个子类,并将其实例分配给dtype“object”(ndarr)的ndarray。它工作得很好,但是当子类recarray用一个空数组实例化时,我遇到了一个问题。这是子类recarry的代码:
class recsub(numpy.recarray):
"""subclassed recarray"""
def __new__(cls, *args, **kwargs):
obj = numpy.recarray.__new__(cls, *args, **kwargs)
return obj
def __init__(self, *arg, **kwargs):
self.x = -1
def new_method(self):
print 'new_method() : fooooooooooooo'
我将ndarray创建为:
^{pr2}$现在,如果我创建两个recsub实例:
^{3}$现在发生了一些奇怪的事情。输出:
print type(ndarr[0])
print type(ndarr[1])
是:
>>> <class '__main__.recsub'>
>>> <class 'numpy.core.records.record'>
所以我无法访问ndarr[1].x
这以前在Numpy1.7中有效,但在Numpy1.8中已经不行了!因此,在使用shape()而不是(n)实例化重新排列时,似乎缺少了一些内容
欢迎提出任何建议
tnx提前
我在Dev1.9中使用更简单的数组得到类似的行为
因此,形状为
()
的数组作为标量插入到ndarr
中。在我不知道这是一个bug、特性,还是1.7到1.8之间的某个变化的预期结果。我想首先要看的是1.8的发行说明。在
此问题可能相关:https://github.com/numpy/numpy/issues/1679
^{pr2}$使用错误修复程序https://github.com/numpy/numpy/pull/4109,以相同的方式返回存储为
array
的项(而不是标量)。在并且OP示例按预期运行。在
相关问题 更多 >
编程相关推荐