0

我正在编写一个脚本,它使用 GPR 来分析和预测不同燃料的燃烧特性。我的测试集有很好的输出,现在想要添加 95% 的置信区间。当我尝试实施间隔时,我得到了可怕的结果。请发送帮助。

#Gaussian Predictions for Ignition Delay
#September 14 2021

%matplotlib inline
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
import seaborn as sns

from sklearn.metrics import mean_absolute_error as mae
from sklearn.model_selection import train_test_split
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import RBF, ConstantKernel as C

#gpr = GaussianProcessRegressor()

kernel = C(1.0, (1e-3, 1e3))*RBF(10, (1e-2, 1e2))
gpr = GaussianProcessRegressor(kernel = kernel, n_restarts_optimizer = 9, alpha = 0.1, normalize_y = True)
gpr.fit(x_train, y_train)
y_prediction, std = gpr.predict(x_test, return_std = True)

confidence = std*1.96/np.sqrt(len(x_test))
confidence = confidence.reshape(-1,1)

# Plot the function, the prediction and the 95% confidence interval based on
# the MSE
plt.figure()

plt.plot(x_train, y_train, "b.", markersize=10, label="Observations")
plt.fill(x_test,
         y_prediction-confidence,
         y_prediction+confidence,
         alpha=0.3,
         fc="b",
         ec="None",
         label="95% confidence interval",
)         #this plots confidence interval and fit it to my data

plt.plot(x_test, y_prediction, "r.", markersize=10, label="Prediction")
```[enter image description here][1]


  [1]: https://i.stack.imgur.com/PItpi.png
4

1 回答 1

0

从 sklearn 文档看这个例子

https://scikit-learn.org/stable/auto_examples/gaussian_process/plot_gpr_noisy_targets.html#sphx-glr-auto-examples-gaussian-process-plot-gpr-noisy-targets-py

看起来您需要调整绘图功能。对我来说,以下工作

plt.fill_between(
    x_test.ravel(),
    y_prediction - 1.96 * std,
    y_prediction + 1.96 * std,
    alpha=0.5,
    label=r"95% confidence interval",
)

在这里,我生成了 sklearn 示例中的数据:

X = np.linspace(start=0, stop=10, num=1_000).reshape(-1, 1)
y = np.squeeze(X * np.sin(X))
rng = np.random.RandomState(1)
training_indices = rng.choice(np.arange(y.size), size=6, replace=False)
test_indices = [x for x in np.arange(y.size) if x not in training_indices]
x_train, y_train = X[training_indices], y[training_indices]
x_test, y_test = X[test_indices], y[test_indices]
于 2021-12-29T08:39:47.160 回答