gpt4 book ai didi

python - 如何在Python中将一维径向轮廓转换为二维数组

转载 作者:太空宇宙 更新时间:2023-11-03 16:03:13 32 4
gpt4 key购买 nike

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

关于python - 如何在Python中将一维径向轮廓转换为二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40115608/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com