在OpenTURNS 中,KrigingAlgorithm 类可以根据特定输入点的已知输出值估计高斯过程模型的超参数。然后KrigingAlgorithm 的getMetamodel 方法返回一个插入数据的函数。
首先,我们需要将 Numpy 数组 coordinates 和 observations 转换为 OpenTURNS Sample 对象:
import openturns as ot
input_train = ot.Sample(coordinates)
output_train = ot.Sample(observations, 1)
数组coordinates 的形状为(6, 2),因此变成了大小为6、维度为2 的Sample。数组observations 的形状为(6,),这是不明确的:大小为 6 且维度为 1 的 Sample,还是大小为 1 且维度为 6 的 Sample?为了澄清这一点,我们在对 Sample 类构造函数的调用中指定维度 (1)。
下面,我们定义一个具有恒定趋势函数和平方指数协方差核的高斯过程模型:
inputDimension = 2
basis = ot.ConstantBasisFactory(inputDimension).build()
covariance_kernel = ot.SquaredExponential([1.0]*inputDimension, [1.0])
algo = ot.KrigingAlgorithm(input_train, output_train,
covariance_kernel, basis)
然后我们将趋势的值和协方差核的参数(幅度参数和尺度参数)进行拟合,得到一个元模型:
# Fit
algo.run()
result = algo.getResult()
krigingMetamodel = result.getMetaModel()
生成的krigingMetamodel 是一个Function,它接受一个二维Point 作为输入并返回一个一维Point。它预测感兴趣的数量。为了说明这一点,让我们构建二维域 [0,1]×[0,1] 并用规则网格对其进行离散化:
# Create the 2D domain
myInterval = ot.Interval([0.0, 0.0], [1.0, 1.0])
# Define the number of interval in each direction of the box
nx = 20
ny = 10
myIndices = [nx - 1, ny - 1]
myMesher = ot.IntervalMesher(myIndices)
myMeshBox = myMesher.build(myInterval)
使用我们的krigingMetamodel 来预测此网格上感兴趣的数量所取的值可以通过以下语句完成。我们首先将网格的vertices 获取为Sample,然后通过对元模型的一次调用来评估预测(这里不需要for 循环):
# Predict
vertices = myMeshBox.getVertices()
predictions = krigingMetamodel(vertices)
为了使用 Matplotlib 查看结果,我们首先必须创建 pcolor 函数所需的数据:
# Format for plot
X = np.array(vertices[:, 0]).reshape((ny, nx))
Y = np.array(vertices[:, 1]).reshape((ny, nx))
predictions_array = np.array(predictions).reshape((ny,nx))
下面的脚本产生了情节:
# Plot
import matplotlib.pyplot as plt
fig = plt.figure()
plt.pcolor(X, Y, predictions_array)
plt.colorbar()
plt.show()
我们看到元模型的预测等于在观察到的输入点的观察。
这个元模型是坐标的平滑函数:它的平滑度随着协方差核平滑度的增加而增加,平方指数协方差核恰好是平滑的。