簡體   English   中英

高斯置信區間:Python

[英]Gaussian Confidence Interval: Python

我正在編寫一個腳本,它使用 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

從 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

看起來你需要調整你的 plot function。 對我來說,以下工作

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]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM