Python 无法减去结构化numpy数组中的特定字段
尝试对结构化numpy数组中的字段进行减法时,出现以下错误:Python 无法减去结构化numpy数组中的特定字段,python,arrays,python-2.7,numpy,Python,Arrays,Python 2.7,Numpy,尝试对结构化numpy数组中的字段进行减法时,出现以下错误: In [8]: print serPos['pos'] - hisPos['pos'] --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-8-8
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')])
如有任何建议,将不胜感激 serPos['pos']的
dtype
是复合的
dtype([('x', '<f8'), ('y', '<f8'), ('z', '<f8')])
我想我们也可以将serPos['pos']
视为一个2d数组(3列)并减去该形式。但是我需要测试语法
serPos['pos'].view((float,(3,)))
应该生成一个(N,3)
2d数组。感谢hpaulj对此的澄清。我还可以通过将np.dtype
更改为raw=np.dtype([('residence',int),('pos','(1,3)f8'))
现在serPos['pos']-hisPos['pos']
工作得很好。干杯我会使用'(3,)f8'
所以serPos['pos']
是(N,3)
,而不是(N,1,3)
。
serPos['pos']['x']-hisPos['pos']['x']
serPos['pos'].view((float,(3,)))