我需要在4个维度(纬度,经度,高度和时间)中线性插值温度数据.
点数相当高(360x720x50x8),我需要一种快速的方法来计算数据范围内空间和时间任意点的温度.
我尝试过使用scipy.interpolate.LinearNDInterpolator但是使用Qhull进行三角测量在矩形网格上效率低下并且需要数小时才能完成.
通过阅读此SciPy票证,解决方案似乎是使用标准实现新的nd插值器interp1d来计算更多数据点,然后使用"最近邻居"方法和新数据集.
然而,这需要很长时间(分钟).
有没有一种快速的方法可以在4维的矩形网格上插入数据而无需花费几分钟才能完成?
我想过使用interp1d4次而不计算更高密度的点,但留给用户用坐标调用,但我无法理解如何做到这一点.
否则我会根据自己的需要编写自己的4D内插器吗?
这是我用来测试这个的代码:
使用scipy.interpolate.LinearNDInterpolator:
import numpy as np
from scipy.interpolate import LinearNDInterpolator
lats = np.arange(-90,90.5,0.5)
lons = np.arange(-180,180,0.5)
alts = np.arange(1,1000,21.717)
time = np.arange(8)
data = np.random.rand(len(lats)*len(lons)*len(alts)*len(time)).reshape((len(lats),len(lons),len(alts),len(time)))
coords = np.zeros((len(lats),len(lons),len(alts),len(time),4))
coords[...,0] = lats.reshape((len(lats),1,1,1))
coords[...,1] = lons.reshape((1,len(lons),1,1))
coords[...,2] = alts.reshape((1,1,len(alts),1))
coords[...,3] = time.reshape((1,1,1,len(time)))
coords = coords.reshape((data.size,4))
interpolatedData = LinearNDInterpolator(coords,data)
Run Code Online (Sandbox Code Playgroud)
使用scipy.interpolate.interp1d:
import numpy as np
from scipy.interpolate import LinearNDInterpolator
lats = np.arange(-90,90.5,0.5) …Run Code Online (Sandbox Code Playgroud) 我想计算数值标量场的 3D 体积积分。
在这篇文章中,我将使用一个可以精确计算积分的示例。因此我选择了以下函数:
在 Python 中,我定义了函数和 3D 中的一组点,然后生成这些点处的离散值:
import numpy as np
# Make data.
def function(x, y, z):
return x**y**z
N = 5
grid = np.meshgrid(
np.linspace(0, 1, N),
np.linspace(0, 1, N),
np.linspace(0, 1, N)
)
points = np.vstack(list(map(np.ravel, grid))).T
x = points[:, 0]
y = points[:, 1]
z = points[:, 2]
values = [function(points[i, 0], points[i, 1], points[i, 2])
for i in range(len(points))]
Run Code Online (Sandbox Code Playgroud)
如果我不知道底层函数,即如果我只有坐标 ( x, y, z) 和 ,我怎样才能找到积分values?
我需要插入多索引数据帧:
例如:
这是主要数据框:
a b c result
1 1 1 6
1 1 2 9
1 2 1 8
1 2 2 11
2 1 1 7
2 1 2 10
2 2 1 9
2 2 2 12
Run Code Online (Sandbox Code Playgroud)
我需要找到以下结果:
1.3 1.7 1.55
Run Code Online (Sandbox Code Playgroud)
到目前为止我一直在做的是为每个索引分别附加一个 pd.Series 和 NaN 。
如你看到的。这似乎是一种非常低效的方式。
如果有人能让我充实,我会很高兴。
PS我花了一些时间查看SO,如果答案在那里,我错过了:
算法:
阶段1:
a b c result
1 1 1 6
1 1 2 9
1 2 …Run Code Online (Sandbox Code Playgroud) 我使用以下脚本在一些(x,y,z)数据上使用LinearNDInterpolator。但是,我不知道如何从插值数据转到以热图形式绘制/显示插值?我是否缺少基于x和y的最小值和最大值设置网格网格的内容?任何帮助或示例都很好!
import numpy as np
import scipy.interpolate
x = np.array([-4386795.73911443, -1239996.25110694, -3974316.43669208,
1560260.49911342, 4977361.53694849, -1996458.01768192,
5888021.46423068, 2969439.36068243, 562498.56468588,
4940040.00457585])
y = np.array([ -572081.11495993, -5663387.07621326, 3841976.34982795,
3761230.61316845, -942281.80271223, 5414546.28275767,
1320445.40098735, -4234503.89305636, 4621185.12249923,
1172328.8107458 ])
z = np.array([ 4579159.6898615 , 2649940.2481702 , 3171358.81564312,
4892740.54647532, 3862475.79651847, 2707177.605241 ,
2059175.83411223, 3720138.47529587, 4345385.04025412,
3847493.83999694])
# Create coordinate pairs
cartcoord = zip(x, y)
# Interpolate
interp = scipy.interpolate.LinearNDInterpolator(cartcoord, z)
Run Code Online (Sandbox Code Playgroud)
编辑:基于@Spinor的解决方案,并使用Python 2.7,以下代码为我提供了我正在寻找的方法(方法1)。有没有办法增加插值点的密度?
不用说,我没想到结果是圆形的,因为(纬度,经度)坐标取自等角投影图。在进一步调查中,我认为这只是映射到不同的投影上。