繁体   English   中英

python插值需要很长时间

[英]python interpolation takes a long time

我用1000 x 1000网格中的scipy.interpolate.griddata进行插值。 当我有一个具有1,000(x,y,z)值的点云时,计算只需要几秒钟。 但现在我有1,000,000个值。 所以我创建了一个循环来从这1,000,000个值中提取1,000个值,如下所示:

p = [...]
z = [...]
#p and z are my lists with 1,000,000 values
p_new = []
z_new = []
for i in range(1000000):
    if condition:
        #condition is True for about 1000 times
        p_new.append(p[i])
        z_new.append(z[i])
print 'loop finished'

points = np.array(p_new)
values = np.array(z_new)
grid_z1 = griddata(points, values, (grid_x, grid_y), method='cubic')
plt.imshow(grid_z1.T, origin='lower')
plt.show()

print len(p_new)返回1000 ,所以我的循环按预期工作。 但是在我的循环结束后,我等了15分钟后取消了我的程序,因为什么都没发生。

所以最后我的问题是:为什么这个计算如此长,尽管在两种情况下(默认为1000个值,1000个值从1000000中提取出来)我有相同数量的值? 在我的输出loop finished我可以看到循环只需要大约10秒,所以它应该与我的循环无关= /

我在这里看不到任何不寻常的事情 - 据我所知,插值所花费的时间大致与点云中元素的数量成正比。

这是一些测试数据:

def fake_data(n):

    # xy coordinates for an n-by-n grid
    grid = np.indices((n,n),dtype=np.float32).reshape(2,-1).T

    # interpolated coordinates
    xy_i = grid.copy()
    # not monotonically increasing
    np.random.shuffle(xy_i)

    # values
    z = np.random.rand(n**2)

    # input coordinates
    xy = grid.copy()
    # not regularly gridded 
    xy += np.random.rand(*xy_i.shape)*0.25

    # pick n random points to use
    inc = np.random.choice(np.arange(n**2),(n,),replace=False)
    xy = grid[inc,:]
    z = z[inc]

    return xy, z, xy_i

在此输入图像描述

对于所有三种方法, N对时间的对数 - 对数图大致是直线,斜率为~2,即它们都需要O(N ^ 2)时间。

在您的情况下,如果您看到线条不是直线但是向上偏离大的N值,则可能表示您遇到了其他一些问题,例如内存不足和按下交换。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM