我在 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 个值和从 1000000 中提取它们的 1000 个值)我有相同数量的值,但为什么这个计算要花这么长时间?在我的输出中 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 值,这可能表明您遇到了一些其他问题,例如内存不足和交换区。
关于python插值需要很长时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19122893/