![](/img/trans.png)
[英]Creating a numpy array of 3D coordinates from three 1D arrays, first index changing fastest
[英]Creating a numpy array of 3D coordinates from three 1D arrays
假设我有三个任意1D数组,例如:
x_p = np.array((1.0, 2.0, 3.0, 4.0, 5.0))
y_p = np.array((2.0, 3.0, 4.0))
z_p = np.array((8.0, 9.0))
这三个数组表示3D网格中的采样间隔,我想为所有交叉点构建三维向量的一维数组,类似于
points = np.array([[1.0, 2.0, 8.0],
[1.0, 2.0, 9.0],
[1.0, 3.0, 8.0],
...
[5.0, 4.0, 9.0]])
订单实际上并不重要。 生成它们的明显方法:
npoints = len(x_p) * len(y_p) * len(z_p)
points = np.zeros((npoints, 3))
i = 0
for x in x_p:
for y in y_p:
for z in z_p:
points[i, :] = (x, y, z)
i += 1
所以问题是......有更快的方法吗? 我看了但没找到(可能只是找不到合适的Google关键字)。
我目前正在使用这个:
npoints = len(x_p) * len(y_p) * len(z_p)
points = np.zeros((npoints, 3))
i = 0
nz = len(z_p)
for x in x_p:
for y in y_p:
points[i:i+nz, 0] = x
points[i:i+nz, 1] = y
points[i:i+nz, 2] = z_p
i += nz
但我觉得我错过了一些聪明的花哨的Numpy方式?
要在上面的示例中使用numpy网格,以下内容将起作用:
np.vstack(np.meshgrid(x_p,y_p,z_p)).reshape(3,-1).T
Numpy meshgrid对于超过两维的网格需要numpy 1.7。 绕过这个并从源代码中提取相关数据。
def ndmesh(*xi,**kwargs):
if len(xi) < 2:
msg = 'meshgrid() takes 2 or more arguments (%d given)' % int(len(xi) > 0)
raise ValueError(msg)
args = np.atleast_1d(*xi)
ndim = len(args)
copy_ = kwargs.get('copy', True)
s0 = (1,) * ndim
output = [x.reshape(s0[:i] + (-1,) + s0[i + 1::]) for i, x in enumerate(args)]
shape = [x.size for x in output]
# Return the full N-D matrix (not only the 1-D vector)
if copy_:
mult_fact = np.ones(shape, dtype=int)
return [x * mult_fact for x in output]
else:
return np.broadcast_arrays(*output)
检查结果:
print np.vstack((ndmesh(x_p,y_p,z_p))).reshape(3,-1).T
[[ 1. 2. 8.]
[ 1. 2. 9.]
[ 1. 3. 8.]
....
[ 5. 3. 9.]
[ 5. 4. 8.]
[ 5. 4. 9.]]
对于上面的例子:
%timeit sol2()
10000 loops, best of 3: 56.1 us per loop
%timeit np.vstack((ndmesh(x_p,y_p,z_p))).reshape(3,-1).T
10000 loops, best of 3: 55.1 us per loop
对于每个维度为100时:
%timeit sol2()
1 loops, best of 3: 655 ms per loop
In [10]:
%timeit points = np.vstack((ndmesh(x_p,y_p,z_p))).reshape(3,-1).T
10 loops, best of 3: 21.8 ms per loop
根据您要对数据执行的操作,您可以返回视图:
%timeit np.vstack((ndmesh(x_p,y_p,z_p,copy=False))).reshape(3,-1).T
100 loops, best of 3: 8.16 ms per loop
对于您的具体示例, mgrid
非常有用:
In [1]: import numpy as np
In [2]: points = np.mgrid[1:6, 2:5, 8:10]
In [3]: points.reshape(3, -1).T
Out[3]:
array([[1, 2, 8],
[1, 2, 9],
[1, 3, 8],
[1, 3, 9],
[1, 4, 8],
[1, 4, 9],
[2, 2, 8],
[2, 2, 9],
[2, 3, 8],
[2, 3, 9],
[2, 4, 8],
[2, 4, 9],
[3, 2, 8],
[3, 2, 9],
[3, 3, 8],
[3, 3, 9],
[3, 4, 8],
[3, 4, 9],
[4, 2, 8],
[4, 2, 9],
[4, 3, 8],
[4, 3, 9],
[4, 4, 8],
[4, 4, 9],
[5, 2, 8],
[5, 2, 9],
[5, 3, 8],
[5, 3, 9],
[5, 4, 8],
[5, 4, 9]])
更一般地说,如果你想要使用特定的数组,你可以使用meshgrid
而不是mgrid
。 但是,你需要numpy 1.7或更高版本才能在两个以上的维度上工作。
您可以使用itertools.product
:
def sol1():
points = np.array( list(product(x_p, y_p, z_p)) )
使用迭代器和np.take
另一个解决方案显示你的情况快了大约3倍:
from itertools import izip, product
def sol2():
points = np.empty((len(x_p)*len(y_p)*len(z_p),3))
xi,yi,zi = izip(*product( xrange(len(x_p)),
xrange(len(y_p)),
xrange(len(z_p)) ))
points[:,0] = np.take(x_p,xi)
points[:,1] = np.take(y_p,yi)
points[:,2] = np.take(z_p,zi)
return points
时间结果:
In [3]: timeit sol1()
10000 loops, best of 3: 126 µs per loop
In [4]: timeit sol2()
10000 loops, best of 3: 42.9 µs per loop
In [6]: timeit ops()
10000 loops, best of 3: 59 µs per loop
In [11]: timeit joekingtons() # with your permission Joe...
10000 loops, best of 3: 56.2 µs per loop
因此,只有我的第二个解决方案和Joe Kington的解决方案才能为您带来一些性能提升......
对于那些不得不忍受numpy <1.7.x的人来说,这是一个简单的双线解决方案:
g_p=np.zeros((x_p.size, y_p.size, z_p.size))
array_you_want=array(zip(*[item.flatten() for item in \
[g_p+x_p[...,np.newaxis,np.newaxis],\
g_p+y_p[...,np.newaxis],\
g_p+z_p]]))
很容易扩展到甚至更高的尺寸。 干杯!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.