简体   繁体   English

Numpy - 总结一个向量列表

[英]Numpy - summing up a list of vectors

I am trying to sum a list of NumPy vectors in a list. 我试图在列表中总结一个NumPy向量列表。 (In this example it's a list of 2 items, but in my case the list can be of any size.) How to sum them into a new vector? (在这个例子中,它是2个项目的列表,但在我的情况下,列表可以是任何大小。)如何将它们加入到新的向量中?

a = np.array([100, 100])
b = np.array([200, 200])
my_list = [a, b]

ab = np.add(my_list)

np.add(a, b) works, but it's not a list. np.add(a, b)有效,但它不是列表。 I have already tried np.add(*my_list) and np.add(np.array(my_list)) as well as np.add(np.array([my_list])) , but without any success. 我已经尝试了np.add(*my_list)np.add(np.array(my_list))以及np.add(np.array([my_list])) ,但没有任何成功。 What would be the correct way to do this? 这样做的正确方法是什么? Thanks! 谢谢!

Solution 1 np.add.reduce() 解决方案1 np.add.reduce()

You can use the reduce attribute of np.add : 您可以使用np.addreduce属性:

a = np.array([100, 100])
b = np.array([200, 200])
c = np.array([1000, 2000])
L = [a, b, c]
np.add.reduce(L)

results in: 结果是:

array([1300, 2300])

All universal function that take two in-arguments have a reduce attribute, that applies this function like reduce , ie: 所有带有两个in-arguments的通用函数都有一个reduce属性,它将这个函数应用为reduce ,即:

np.add.reduce(L)

becomes: 变为:

np.add(np.add(L[0], L[1]), L[2])

Add more parenthesis and appropriate np.add calls if the list L gets larger. 如果列表L变大,则添加更多括号和相应的np.add调用。

From the docs: 来自文档:

Docstring: 文档字符串:

  reduce(a, axis=0, dtype=None, out=None, keepdims=False) 

Reduces a 's dimension by one, by applying ufunc along one axis. 通过沿一个轴应用ufunc将a的维数减1。

Solution 2 np.sum() 解决方案2 np.sum()

Alternatively, you can use np.sum along the first axis: 或者,您可以沿第一个轴使用np.sum

>>> np.sum(L, axis=0)
array([1300, 2300

Performance 性能

The performance of both seems to be the same. 两者的表现似乎都是一样的。

For small arrays: 对于小型阵列:

a = np.array([100, 100])
b = np.array([200, 200])
c = np.array([1000, 2000])
L = [a, b, c, a, b, c, a, b, c]

reduce is a little bit faster: reduce更快一点:

%timeit np.sum(L, axis=0)

10000 loops, best of 3: 20.7 µs per loop

%timeit np.add.reduce(L)
100000 loops, best of 3: 15.7 µs per loop

For large arrays: 对于大型阵列:

size = int(1e6)
a = np.random.random(size)
b = np.random.random(size)
c = np.random.random(size)
L = [a, b, c, a, b, c, a, b, c]

There is no difference: 没有区别:

%timeit np.sum(L, axis=0)
10 loops, best of 3: 41.5 ms per loop

%timeit np.add.reduce(L)
10 loops, best of 3: 41.9 ms per loop

Is this what you mean? 你是这个意思吗?

import numpy as np

a = np.array([100, 100])
b = np.array([200, 200])
my_list = [a, b]

# add them up "vertically"

print np.vstack(my_list).sum(axis=0)

print np.vstack(tuple(my_list)).sum(axis=0)  # I thought it had to be a tuple but apparently not!


[300 300]
[300 300]

You could use np.hstack or np.concatenate : 你可以使用np.hstacknp.concatenate

l = [a, b]

In [560]: np.hstack(l)
Out[560]: array([100, 100, 200, 200])

In [561]: np.concatenate(l)
Out[561]: array([100, 100, 200, 200])

Probably an ideal candidate for reduce 可能是减少的理想候选人

>>> a = np.array([100, 100])
>>> b = np.array([200, 200])
>>> c = np.array([300, 300])
>>> reduce(lambda x,y: np.add(x,y), [a,b,c])
array([600, 600])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM