我希望每个人都健康并保持安全。
我目前有以下情节。我想最终在x = 0,y = 0处绘制这样的高斯分布。橙色线基本上是95%的置信区间。
我应该尝试交换轴并绘图吗?有更好的方法吗?我目前正在使用matplotlib和python进行绘图。是否有更好的库可以绘制?请告诉我。
谢谢!
我有以下代码:
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import RBF, ConstantKernel
noise = 1.0
X = np.arange(0, 1, 0.01).reshape(-1, 1)
kernel = ConstantKernel(10**2) * RBF(length_scale=0.35)
gp = GaussianProcessRegressor(kernel=kernel, alpha=noise**2, optimizer=None)
gp_mean, gp_std = gp.predict(X, return_std=True)
# Create the figure and the axes
fig, ax = plt.subplots()
ax.plot(X, gp_mean, 'k-', label='Zero-Mean GP')
ax.fill_between(X.ravel(), gp_mean + 1.96*gp_std, gp_mean - 1.96*gp_std, alpha=0.30, label='95% confidence interval')
ax.grid()
ax.legend(prop={'size': 12})
ax.set_xlim([-0.02, 1.0])
ax.set_ylim([-30.0, 30.0])
ax.tick_params(axis='both', labelsize=14)
ax.set_xlabel(r'$x$', fontsize=14)
plt.show()
您可以在y轴上绘制高斯法线的pdf,如下所示:
import numpy as np
from scipy import stats
from matplotlib import pyplot as plt
gp_mean = 0
gp_std = 12
gaussian = stats.norm(gp_mean, gp_std)
fig, ax = plt.subplots()
ys = np.linspace(*gaussian.ppf([0.001, 0.999]), 200)
ax.plot(gaussian.pdf(ys), ys, color='deepskyblue', label='gaussian normal')
ax.axhspan(*gaussian.ppf([0.05, 0.95]), color='chocolate', alpha=0.2, label='95% confidence interval')
ax.plot(0, gp_mean, marker='o', color='crimson', label='mean')
ax.set_xlim(0, 0.5)
ax.legend(prop={'size': 12})
plt.show()
PS:要同时绘制pdf和x = 0.5
,请添加:
ax.plot(0.5 + gaussian.pdf(ys), ys, color='deepskyblue')
ax.plot(0.5, gp_mean, marker='o', color='crimson')
ax.set_xlim(0, 1)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句