我将尝试使用OpenTURNS 平台回答您的问题。
假设西班牙是一个 1000 x 1000 公里的正方形,并且您的 500 个点随机分布在表面上
import openturns as ot
import numpy as np
# initiate a sample of size 500 with 2 coordinates
inputdata = ot.Sample(500, 2)
# 1st column random between 0 and 1000
inputdata[:,0] = ot.Uniform(0,1000).getSample(500)
# 2nd column random between 0 and 1000
inputdata[:,1] = ot.Uniform(0,1000).getSample(500)
然后让我们为这些点中的每一个指定一个高度。OpenTURNS 允许定义符号函数:
height = ot.SymbolicFunction(["x","y"], ["10 +10 * (x + y) / 1000 + 10 * ((x + y) / 1000) * sin( 3 * x * pi_ / 1000 )*cos(5 * y * pi_ / 1000)"])
outputdata = height(inputdata)
现在我们想对数据进行插值以估计地图上任意点的高度。克里金法允许这样做,但您最好了解有关您的问题的一些信息(一般趋势,2 个远处点的高度之间的相关性)。
# dimension of the input data
dimension = 2
basis = ot.ConstantBasisFactory(dimension).build()
covarianceModel = ot.SquaredExponential(dimension)
然后我们只调用克里金算法进行插值
algo = ot.KrigingAlgorithm(inputdata, outputdata, covarianceModel, basis)
algo.run()
result = algo.getResult()
metamodel = result.getMetaModel()
metamodel
正是你想要的功能!
# gives the inferred height of the point (x = 123, y = 967)
metamodel([123, 967])
>>> [12.2225]
如果您想绘制结果,您可以在正方形的网格上计算预测值
gridx = np.arange(0.0,1001,10)
nx = len(gridx)
gridy = np.arange(0.0,1001,10)
ny = len(gridx)
X, Y = np.meshgrid(gridx, gridy)
predictions = np.array(metamodel([[xi,yi] for (xi, yi) in zip(X.ravel(),Y.ravel())])).reshape(nx,ny)
然后您可以使用 matplotlib 查看结果:
import matplotlib.pylab as plt
plt.figure()
vmin = predictions.min()
vmax = predictions.max()
plt.pcolor(X, Y, predictions, cmap='viridis', vmin=vmin, vmax=vmax)
plt.scatter([d[0] for d in inputdata], [d[1] for d in inputdata], c = [d for d in outputdata], s=2, edgecolor = "white", cmap='viridis', vmin=vmin, vmax=vmax)
plt.colorbar()
plt.show()
您还可以在 3D 中查看它:-)
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
fig = plt.figure()
ax = fig.gca(projection='3d')
surf = ax.plot_surface(X, Y, predictions, cmap=cm.viridis,
linewidth=0, antialiased=False)
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()