2
我想子类numpy recarrays并能够创建该类的视图。例如:创建视图重新排列子类
_Theta = np.dtype([('v', '<f8'), ('a', '<f8'), ('w', '<f8')])
class Theta(np.recarray):
''' Defines a class for the parameters of a DDM model. '''
def __new__(clc, shape=1):
return np.recarray.__new__(clc, shape, _Theta)
如果我创建一个实例,它会生成我想要的结果。
In [12]: r = Theta()
In [13]: r
Out[13]:
Theta([(6.91957786449907e-310, 2.9958354e-316, 2.0922526e-316)],
dtype=[('v', '<f8'), ('a', '<f8'), ('w', '<f8')])
但是,如果我尝试创建我的课的看法,我得到如下:
In [11]: np.zeros(3).view(Theta).a
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-11-b9bdab6cd66c> in <module>()
----> 1 np.zeros(3).view(Theta).a
/usr/lib/python2.7/dist-packages/numpy/core/records.pyc in __getattribute__(self, attr)
414 res = fielddict[attr][:2]
415 except (TypeError, KeyError):
--> 416 raise AttributeError("record array has no attribute %s" % attr)
417 obj = self.getfield(*res)
418 # if it has fields return a recarray, otherwise return
AttributeError: record array has no attribute a
是否有可能创建视图的阵列,这样我可以有属性解决他们我定义了Theta? (我认为这应该通过array_finalize方法以某种方式完成,但我不知道如何)。