使用 numpy 构建两个数组的所有组合的数组 [英] Using numpy to build an array of all combinations of two arrays
问题描述
在尝试用它做任何复杂的事情之前,我试图遍历 6 参数函数的参数空间来研究它的数值行为,所以我正在寻找一种有效的方法来做到这一点.
I'm trying to run over the parameters space of a 6 parameter function to study its numerical behavior before trying to do anything complex with it, so I'm searching for an efficient way to do this.
我的函数将 6 维 numpy 数组中给出的浮点值作为输入.我最初尝试做的是:
My function takes float values given in a 6-dim numpy array as input. What I tried to do initially was this:
首先,我创建了一个函数,该函数接受 2 个数组并生成一个包含来自两个数组的所有值组合的数组:
First, I created a function that takes 2 arrays and generate an array with all combinations of values from the two arrays:
from numpy import *
def comb(a,b):
c = []
for i in a:
for j in b:
c.append(r_[i,j])
return c
然后,我使用 reduce()
将其应用于同一数组的 m 个副本:
Then, I used reduce()
to apply that to m copies of the same array:
def combs(a,m):
return reduce(comb,[a]*m)
最后,我这样评估我的函数:
Finally, I evaluate my function like this:
values = combs(np.arange(0,1,0.1),6)
for val in values:
print F(val)
这行得通,但方式太慢了.我知道参数的空间很大,但这不应该那么慢.在这个例子中,我只采样了 106(一百万)个点,仅仅创建数组 values
就花了超过 15 秒.
This works but it's way too slow. I know the space of parameters is huge, but this shouldn't be so slow. I have only sampled 106 (a million) points in this example and it took more than 15 seconds just to create the array values
.
您知道使用 numpy 执行此操作的更有效方法吗?
Do you know any more efficient way of doing this with numpy?
如果需要,我可以修改函数 F
接受它的参数的方式.
I can modify the way the function F
takes it's arguments if it's necessary.
推荐答案
在较新版本的 numpy
(>1.8.x) 中,numpy.meshgrid()
提供了更快的实现:
In newer version of numpy
(>1.8.x), numpy.meshgrid()
provides a much faster implementation:
@pv 的解决方案
In [113]:
%timeit cartesian(([1, 2, 3], [4, 5], [6, 7]))
10000 loops, best of 3: 135 µs per loop
In [114]:
cartesian(([1, 2, 3], [4, 5], [6, 7]))
Out[114]:
array([[1, 4, 6],
[1, 4, 7],
[1, 5, 6],
[1, 5, 7],
[2, 4, 6],
[2, 4, 7],
[2, 5, 6],
[2, 5, 7],
[3, 4, 6],
[3, 4, 7],
[3, 5, 6],
[3, 5, 7]])
numpy.meshgrid()
以前只有 2D,现在可以进行 ND.在这种情况下,3D:
numpy.meshgrid()
use to be 2D only, now it is capable of ND. In this case, 3D:
In [115]:
%timeit np.array(np.meshgrid([1, 2, 3], [4, 5], [6, 7])).T.reshape(-1,3)
10000 loops, best of 3: 74.1 µs per loop
In [116]:
np.array(np.meshgrid([1, 2, 3], [4, 5], [6, 7])).T.reshape(-1,3)
Out[116]:
array([[1, 4, 6],
[1, 5, 6],
[2, 4, 6],
[2, 5, 6],
[3, 4, 6],
[3, 5, 6],
[1, 4, 7],
[1, 5, 7],
[2, 4, 7],
[2, 5, 7],
[3, 4, 7],
[3, 5, 7]])
请注意,最终结果的顺序略有不同.
Note that the order of the final resultant is slightly different.
这篇关于使用 numpy 构建两个数组的所有组合的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!