【发布时间】:2016-12-07 22:40:18
【问题描述】:
在尝试减去结构化 numpy 数组中的字段时,出现以下错误:
In [8]: print serPos['pos'] - hisPos['pos']
---------------------------------------------------------------------------
TypeError
Traceback (most recent call last) <ipython-input-8-8a22559cfb2d> in <module>()
----> 1 print serPos['pos'] - hisPos['pos']
TypeError: ufunc 'subtract' did not contain a loop with signature matching types
dtype([('x', '<f8'), ('y', '<f8'), ('z', '<f8')])
dtype([('x', '<f8'), ('y', '<f8'), ('z', '<f8')])
dtype([('x', '<f8'), ('y', '<f8'), ('z', '<f8')])
鉴于标准 float dtype,为什么我无法执行此减法?
为了重现这些情况,提供了以下示例代码:
import numpy as np
raw = np.dtype([('residue', int),
('pos', [('x', float),
('y', float),
('z', float)])])
serPos = np.empty([0,2],dtype=raw)
hisPos = np.empty([0,2],dtype=raw)
serPos = np.append(serPos, np.array([(1,(1,2,3))], dtype=raw))
hisPos = np.append(hisPos, np.array([(1,(1,2,3))], dtype=raw))
print serPos['pos'], hisPos['pos'] # prints fine
print serPos['pos'] - hisPos['pos'] # errors with ufunc error
任何建议将不胜感激!
【问题讨论】:
标签: python arrays python-2.7 numpy