英文:
How to get a sum of a structured numpy array?
问题
我正在尝试获取结构化数组的总和。我是否需要在字段名称周围使用 "for" 循环,还是可以在一行中完成?我每次运行大约一百万次,所以需要保持速度快。
test = np.array([(1,2,3),(4,5,6)], dtype={'names': ["One", "Two", "Three"], 'formats': ["f8"]*3})
test
np.sum(test)
我期望得到一个结构化数组:[(5, 7, 9), dtype=...]。至少,没有错误。
英文:
I am trying to get the sum of a structured array. Do a need a "for" loop around the field names, or can I do it in one line. I do this about a million times per run, so I need to keep it quick.
>>> test = np.array([(1,2,3),(4,5,6)], dtype={'names' : ["One", "Two", "Three"], 'formats' : ["f8"]*3})
>>> test
array([(1., 2., 3.), (4., 5., 6.)],
dtype=[('One', '<f8'), ('Two', '<f8'), ('Three', '<f8')])
>>> np.sum(test)
...
numpy.core._exceptions._UFuncNoLoopError: ufunc 'add' did not contain a loop with signature matching types (dtype([('One', '<f8'), ('Two', '<f8'), ('Three', '<f8')]), dtype([('One', '<f8'), ('Two', '<f8'), ('Three', '<f8')])) -> None
similar for np.sum(test, axis=0)
I was expecting a structured array: [(5, 7, 9), dtype=...]. At least, no errors.
答案1
得分: 0
在这种情况下,由于您的结构具有相同的原始类型,您可以轻松使用视图:
import numpy as np
test = np.array([(1,2,3),(4,5,6)], dtype={'names': ["One", "Two", "Three"], 'formats': ["f8"]*3})
test.view('f8').reshape(-1,3).sum(axis=0)
# 输出:array([5., 7., 9.])
现在,如果您无法轻松从原始的结构化dtype中创建视图,您可以通过对字段进行循环来使用,这不应该太慢:
result = np.zeros_like(test, shape=(1,))
for field in test.dtype.fields:
result[field] = test[field].sum()
result
# 输出:array([(5., 7., 9.)],
# dtype=[('One', '<f8'), ('Two', '<f8'), ('Three', '<f8')])
英文:
So, in this case, since your structure has the same primitive types, you can use a view easily enough:
>>> import numpy as np
>>> test = np.array([(1,2,3),(4,5,6)], dtype={'names' : ["One", "Two", "Three"], 'formats' : ["f8"]*3})
>>> test.view('f8').reshape(-1,3).sum(axis=0)
array([5., 7., 9.])
Now, if you couldn't easily create a view from your original structured dtype, you could use a loop over the fields, which shouldn't be terribly slow:
>>> result = np.zeros_like(test, shape=(1,))
>>> for field in test.dtype.fields:
... result[field] = test[field].sum()
...
>>> result
array([(5., 7., 9.)],
dtype=[('One', '<f8'), ('Two', '<f8'), ('Three', '<f8')])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论