像point_type
这样的结构化数组没有定义涉及多个字段的数学运算。
随着样品从https://stackoverflow.com/a/33455682/901925
In [470]: point_type = [('x', float), ('y', float)]
In [471]: points = np.array([(1,2), (3,4), (5,6)], dtype=point_type)
In [472]: points
Out[472]:
array([(1.0, 2.0), (3.0, 4.0), (5.0, 6.0)],
dtype=[('x', '<f8'), ('y', '<f8')])
In [473]: points[0]+points[1]
...
TypeError: unsupported operand type(s) for +: 'numpy.void' and 'numpy.void'
相反,我可以创建一个二维数组,然后将其视为point_type
- 设置DataBuffer布局将是相同的:
In [479]: points = np.array([(1,2), (3,4), (5,6)],float)
In [480]: points
Out[480]:
array([[ 1., 2.],
[ 3., 4.],
[ 5., 6.]])
In [481]: points.view(point_type)
Out[481]:
array([[(1.0, 2.0)],
[(3.0, 4.0)],
[(5.0, 6.0)]],
dtype=[('x', '<f8'), ('y', '<f8')])
In [482]: points.view(point_type).view(np.recarray).x
Out[482]:
array([[ 1.],
[ 3.],
[ 5.]])
我可以做数学横跨行,并继续以点的形式查看结果:
In [483]: (points[0]+points[1]).view(point_type).view(np.recarray)
Out[483]:
rec.array([(4.0, 6.0)],
dtype=[('x', '<f8'), ('y', '<f8')])
In [484]: _.x
Out[484]: array([ 4.])
In [485]: points.sum(0).view(point_type)
Out[485]:
array([(9.0, 12.0)],
dtype=[('x', '<f8'), ('y', '<f8')])
我也可以与point_type
开始,并认为它是2D的数学,然后视图它回
pdt1=np.dtype((float, (2,)))
In [502]: points
Out[502]:
array([(1.0, 2.0), (3.0, 4.0), (5.0, 6.0)],
dtype=[('x', '<f8'), ('y', '<f8')])
In [503]: points.view(pdt1)
Out[503]:
array([[ 1., 2.],
[ 3., 4.],
[ 5., 6.]])
In [504]: points.view(pdt1).sum(0).view(point_type)
Out[504]:
array([(9.0, 12.0)],
dtype=[('x', '<f8'), ('y', '<f8')])
因此,有可能查看和在阵列上作为2d和作为recarray操作。要是漂亮或有用,它可能需要在用户定义的类中进行埋设。
另一种选择从recarray
类中挑选想法的选项。在它的核心,它只是一个结构化数组,其中包含专门的__getattribute__
(和setattribute)方法。该方法首先检索正常的数组方法和属性(例如x.shape
,x.sum
)。然后它会尝试在定义的字段名中对attr
进行罚款。
def __getattribute__(self, attr):
try:
return object.__getattribute__(self, attr)
except AttributeError: # attr must be a fieldname
pass
fielddict = ndarray.__getattribute__(self, 'dtype').fields
try:
res = fielddict[attr][:2]
except (TypeError, KeyError):
raise AttributeError("record array has no attribute %s" % attr)
return self.getfield(*res)
...
points.view(np.recarray).x
变得points.getfield(*points.dtype.fields['x'])
。
一种替代方法将是从namedtuple
(/usr/lib/python3.4/collections/__init__.py
)借用,并定义x
和y
性质,这将索引2D阵列的[:,0]
和[:,1]
列。 将这些属性添加到np.matrix
的子类可能是最容易的,让该类确保大多数数学结果是2d。
'np.recarray'是结构化数组,允许您以字段的形式访问字段 - 对命名的等价物进行排序。但是你不能轻易地在结构化数组上执行2D数学。 – hpaulj