在numpy中生成一个n维坐标数组

13

假设我有一个函数f,它可以将坐标作为参数并返回一个整数(在这种情况下是f(x))。坐标可以是多维的,以列表的形式表示。我的目标是使用两个坐标之间的所有值来填充numpy数组。我尝试过制作所有可能索引的列表,并将其用作向量化函数的输入。

这是我针对二维坐标编写的代码:

import itertools
import numpy


def index_array(lower_corner, upper_corner):
     x_range = range(lower_corner[0], upper_corner[0])
     y_range = range(lower_corner[1], upper_corner[1])
     return numpy.array(list(itertools.product(x_range, y_range)))


print(index_array([2, -2], [5, 3]))

这将返回预期的索引列表:

[[ 2 -2]
 [ 2 -1]
 [ 2  0]
 [ 2  1]
 [ 2  2]
 [ 3 -2]
 [ 3 -1]
 [ 3  0]
 [ 3  1]
 [ 3  2]
 [ 4 -2]
 [ 4 -1]
 [ 4  0]
 [ 4  1]
 [ 4  2]]

以下是我对于n维度的尝试:

import itertools
import numpy


def f(x):
    # dummy function
    return x + 5


def index_array(lower_corner, upper_corner):
    # returns all indices between two n-dimensional points
    range_list = []
    for n in range(len(lower_corner)):
        range_list.append(range(lower_corner[n], upper_corner[n]))
    return numpy.array(list(itertools.product(*range_list)))


lower_corner = numpy.array([2, -2])
upper_corner = numpy.array([5, 3])
indices = index_array(lower_corner, upper_corner)
vect_func = numpy.vectorize(f)
results = vect_func(indices)
print(results)

虽然这样可以运行,但是速度非常慢,需要大量的内存。有没有可能以更高效的方式编写代码呢?我考虑使用numpy.meshgrid,但我不知道该如何使用它。

2个回答

9

确实,使用np.meshgrid是一种通过一些stacking来完成的方法,如下所示 -

def ndim_grid(start,stop):
    # Set number of dimensions
    ndims = len(start)

    # List of ranges across all dimensions
    L = [np.arange(start[i],stop[i]) for i in range(ndims)]

    # Finally use meshgrid to form all combinations corresponding to all 
    # dimensions and stack them as M x ndims array
    return np.hstack((np.meshgrid(*L))).swapaxes(0,1).reshape(ndims,-1).T

范例运行

1) 2D 情形:

In [97]: ndim_grid([2, -2],[5, 3])
Out[97]: 
array([[ 2, -2],
       [ 2, -1],
       [ 2,  0],
       [ 2,  1],
       [ 2,  2],
       [ 3, -2],
       [ 3, -1],
       [ 3,  0],
       [ 3,  1],
       [ 3,  2],
       [ 4, -2],
       [ 4, -1],
       [ 4,  0],
       [ 4,  1],
       [ 4,  2]])

2) 3D案例:

In [98]: ndim_grid([2, -2, 4],[5, 3, 6])
Out[98]: 
array([[ 2, -2,  4],
       [ 2, -2,  5],
       [ 2, -1,  4],
       [ 2, -1,  5],
       [ 2,  0,  4],
       [ 2,  0,  5],
       [ 2,  1,  4],
       [ 2,  1,  5],
       [ 2,  2,  4],
       [ 2,  2,  5],
       [ 3, -2,  4],
       [ 3, -2,  5],
       [ 3, -1,  4],
       [ 3, -1,  5],
       [ 3,  0,  4],
       [ 3,  0,  5],
       [ 3,  1,  4],
       [ 3,  1,  5],
       [ 3,  2,  4],
       [ 3,  2,  5],
       [ 4, -2,  4],
       [ 4, -2,  5],
       [ 4, -1,  4],
       [ 4, -1,  5],
       [ 4,  0,  4],
       [ 4,  0,  5],
       [ 4,  1,  4],
       [ 4,  1,  5],
       [ 4,  2,  4],
       [ 4,  2,  5]])

3
另一个选项是使用来自itertools的product,如果角点高于2D,这也适用:
import itertools as it
lower_corner = [2, -2]
upper_corner = [5, 3]
[coord for coord in it.product(*[range(r[0], r[1]) for r in zip(lower_corner, upper_corner)])]

[(2, -2),
 (2, -1),
 (2, 0),
 (2, 1),
 (2, 2),
 (3, -2),
 (3, -1),
 (3, 0),
 (3, 1),
 (3, 2),
 (4, -2),
 (4, -1),
 (4, 0),
 (4, 1),
 (4, 2)]

额,我已经在使用 itertools 中的 product 了。它被隐藏在第二个函数的返回值中:D。但感谢更紧凑的循环! - Gnarflord
1
你可以用 range(*r) 替换 range(r[0], r[1])。同时,你正在使用一个立即解包的列表推导式。你可以使用生成器表达式代替(即将 [] 替换为 ()),避免创建临时列表。 - Bakuriu
@Bakuriu 工作得非常完美,感谢您指出。我相信这是一种更高效、更简洁的解决方案。 - Psidom
谢谢,这是一个非常优雅和简单的解决方案。我会补充一点:如果已经有一个向量列表,只需将该列表替换为 it.product(*my_vector_list),并且放弃均匀间距的生成器/列表。这可以推广到非均匀间距向量的情况。 - aeolus

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接