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

2024-06-16 10:38:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个列表,它模拟了一个与半径成函数的现象。我想把它转换成二维数组。我写了一些代码,完全符合我的要求,但是由于它使用嵌套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)]

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


Tags: 函数代码in列表forlen半径数组
1条回答
网友
1楼 · 发布于 2024-06-16 10:38:49

下面是一个使用^{}-

a = np.arange(critDim)**2
r2D = np.sqrt(a[:,None] + a)
out = profile1D[(l+r2D).astype(int)]

如果l+r2D生成了许多重复索引,我们可以使用^{}进一步提高性能,如-

^{pr2}$

运行时测试

函数定义-

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

相关问题 更多 >