如何在python中将1D径向轮廓转换为2D数组



我有一个列表,模型的现象是一个半径的函数。我想把它转换成二维数组。我写了一些代码来做我想做的事情,但是因为它使用嵌套的for循环,所以相当慢。

l = len(profile1D)/2
critDim = int((l**2 /2.)**(1/2.))
profile2D = np.empty([critDim, critDim])
for x in xrange(0, critDim):
    for y in xrange(0,critDim):
        r = ((x**2 + y**2)**(1/2.))
        profile2D[x,y] = profile1D[int(l+r)]

是否有更有效的方法来避免这些循环?

这是使用broadcasting -

的矢量化方法
a = np.arange(critDim)**2
r2D = np.sqrt(a[:,None] + a)
out = profile1D[(l+r2D).astype(int)]

如果有很多由l+r2D生成的重复索引,我们可以使用np.take来进一步提高性能,如-

out = np.take(profile1D,(l+r2D).astype(int))
运行时测试

函数定义-

def org_app(profile1D,l,critDim):
    profile2D = np.empty([critDim, critDim])
    for x in xrange(0, critDim):
        for y in xrange(0,critDim):
            r = ((x**2 + y**2)**(1/2.))
            profile2D[x,y] = profile1D[int(l+r)]
    return profile2D
def vect_app1(profile1D,l,critDim):
    a = np.arange(critDim)**2
    r2D = np.sqrt(a[:,None] + a)
    out = profile1D[(l+r2D).astype(int)]
    return out
def vect_app2(profile1D,l,critDim):
    a = np.arange(critDim)**2
    r2D = np.sqrt(a[:,None] + a)
    out = np.take(profile1D,(l+r2D).astype(int))
    return out

计时和验证-

In [25]: # Setup input array and params
    ...: profile1D = np.random.randint(0,9,(1000))
    ...: l = len(profile1D)/2
    ...: critDim = int((l**2 /2.)**(1/2.))
    ...: 
In [26]: np.allclose(org_app(profile1D,l,critDim),vect_app1(profile1D,l,critDim))
Out[26]: True
In [27]: np.allclose(org_app(profile1D,l,critDim),vect_app2(profile1D,l,critDim))
Out[27]: True
In [28]: %timeit org_app(profile1D,l,critDim)
10 loops, best of 3: 154 ms per loop
In [29]: %timeit vect_app1(profile1D,l,critDim)
1000 loops, best of 3: 1.69 ms per loop
In [30]: %timeit vect_app2(profile1D,l,critDim)
1000 loops, best of 3: 1.68 ms per loop
In [31]: # Setup input array and params
    ...: profile1D = np.random.randint(0,9,(5000))
    ...: l = len(profile1D)/2
    ...: critDim = int((l**2 /2.)**(1/2.))
    ...: 
In [32]: %timeit org_app(profile1D,l,critDim)
1 loops, best of 3: 3.76 s per loop
In [33]: %timeit vect_app1(profile1D,l,critDim)
10 loops, best of 3: 59.8 ms per loop
In [34]: %timeit vect_app2(profile1D,l,critDim)
10 loops, best of 3: 59.5 ms per loop

相关内容

  • 没有找到相关文章

最新更新