如何获取一个结构化的NumPy数组的总和?

huangapple go评论94阅读模式
英文:

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:

&gt;&gt;&gt; import numpy as np
&gt;&gt;&gt; test = np.array([(1,2,3),(4,5,6)], dtype={&#39;names&#39; : [&quot;One&quot;, &quot;Two&quot;, &quot;Three&quot;], &#39;formats&#39; : [&quot;f8&quot;]*3})
&gt;&gt;&gt; test.view(&#39;f8&#39;).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:

&gt;&gt;&gt; result = np.zeros_like(test, shape=(1,))
&gt;&gt;&gt; for field in test.dtype.fields:
...     result[field] = test[field].sum()
...
&gt;&gt;&gt; result
array([(5., 7., 9.)],
      dtype=[(&#39;One&#39;, &#39;&lt;f8&#39;), (&#39;Two&#39;, &#39;&lt;f8&#39;), (&#39;Three&#39;, &#39;&lt;f8&#39;)])

huangapple
  • 本文由 发表于 2023年8月5日 06:10:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76839353.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定