简体   繁体   English

Scipy 对结构化二维数据进行插值,但在非结构化点进行评估?

[英]Scipy interpolate on structured 2d data, but evaluate at unstructured points?

I have the following minimum code using scipy.interpolate.interp2d to do interpolation on 2d grid data.我有以下最小代码使用scipy.interpolate.interp2d对二维网格数据进行插值。

import numpy as np
from scipy import interpolate
x = np.arange(-5.01, 5.01, 0.25)
y = np.arange(-5.01, 5.01, 0.25)
xx, yy = np.meshgrid(x, y)
z = np.sin(xx**2+yy**2)
f = interpolate.interp2d(x, y, z, kind='cubic')

Now f here can be used to evaluate other points.现在这里的f可用于评估其他点。 The problem is the points I want to evaluate are totally random points not forming a regular grid.问题是我要评估的点是完全随机的点,而不是形成规则网格。

# Evaluate at point (x_new, y_new), in total 256*256 points
x_new = np.random.random(256*256)
y_new = np.random.random(256*256)
func(x_new, y_new)

This will cause a runtime error in my PC, it seems to treat x_new and y_new as mesh grid, generate a evaluation matrix 65536x65536, which is not my purpose.这会在我的电脑中导致运行时错误,它似乎将 x_new 和 y_new 视为网格,生成一个评估矩阵 65536x65536,这不是我的目的。

RuntimeError: Cannot produce output of size 65536x65536 (size too large)

One way to get things done is to evaluate points one by one , using code:完成任务的一种方法是使用代码逐个评估点

z_new = np.array([f(i, j) for i, j in zip(x_new, y_new)])

However, it is slow !!!然而,它很!!!

%timeit z_new = np.array([f(i, j) for i, j in zip(x_new, y_new)])

1.26 s ± 46.3 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

Is there any faster way to evaluate random points?有没有更快的方法来评估随机点?

Faster here I mean comparable with time below:在这里更快,我的意思是与下面的时间相当:

x_new = np.random.random(256)
y_new = np.random.random(256)

%timeit f(x_new, y_new)

Same 256*256 = 65536 evaluations, time for this in my PC:相同的 256*256 = 65536 次评估,是时候在我的 PC 上进行了:

1.21 ms ± 39.6 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

It does not have to be in comparable speed with 1.21ms, 121 ms is totally acceptable.它不必与 1.21ms 处于可比的速度,121 ms 是完全可以接受的。

The function you are looking for is scipy.interpolate.RegularGridInterpolator您正在寻找的功能是scipy.interpolate.RegularGridInterpolator

Given a set of points (x,y,z), where x & y are defined on a regular grid, it allows you to sample the z-value of intermediate (x,y) points.给定一组点 (x,y,z),其中 x & y 在规则网格上定义,它允许您对中间 (x,y) 点的 z 值进行采样。 In your case, this would look as follows在您的情况下,这将如下所示

import numpy as np
from scipy import interpolate
x = np.arange(-5.01, 5.01, 0.25)
y = np.arange(-5.01, 5.01, 0.25)

def f(x,y):
    return np.sin(x**2+y**2)

z = f(*np.meshgrid(x, y, indexing='ij', sparse=True))
func = interpolate.RegularGridInterpolator((x,y), z)

x_new = np.random.random(256*256)
y_new = np.random.random(256*256)
xy_new = list(zip(x_new,y_new))
z_new = func(xy_new)func(xy_new)

For more details, see https://docs.scipy.org/doc/scipy-0.16.1/reference/generated/scipy.interpolate.RegularGridInterpolator.html有关更多详细信息,请参阅https://docs.scipy.org/doc/scipy-0.16.1/reference/generated/scipy.interpolate.RegularGridInterpolator.html

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

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