Skip to main content
 首页 » 编程设计

python之scikit-learn/Gaussian Process 不是尺度不变的

2025年04月02日14bonelee

我正在使用库 scikit-learn 测试高斯过程回归,我对它给我的置信区间不满意。 这让我意识到这些不是尺度不变的:如果函数按比例放大(在每个轴上按比例),置信区间就会变得更大。

也许图片会更好地解释它: (蓝点为采样点,绿色为真实函数,蓝色为近似值,置信区间=均值+/- 2sd=灰色区域)

函数缩放 x 1:

函数缩放 x 100:

直觉上,这些置信区间应该是尺度不变的,对吧?我们与其他图书馆获得同样的东西吗?

提前致谢!

PS:代码

# -*- coding: utf-8 -*- 
""" 
Created on Thu May 12 16:12:38 2016 
 
@author: pierre 
""" 
 
import numpy as np 
from sklearn import gaussian_process 
import matplotlib.pyplot as plt 
from mpl_toolkits.mplot3d import Axes3D 
pi=3.14 
 
#Figure 
fig = plt.figure() 
ax = fig.add_subplot(111) 
 
#Function definition 
def f(x): 
    return 3*((x-0.5)**2)*((np.cos(pi*x))**2)+0.2*np.sin(5*x) 
 
# Coefficient of scale 
nn=100  
 
#Real function points 
x_real=np.linspace(0,nn,100) 
y_real=nn*f(x_real/nn).ravel() 
 
#Four points sampled 
X = nn*np.atleast_2d([0.,.2,.5,1.]).T 
y = nn*f(X/nn).ravel() 
 
#For the approximation 
x = np.atleast_2d(np.linspace(0, nn, 200)).T 
 
#GP call 
gp = gaussian_process.GaussianProcess() 
gp.fit(X, y)   
y_pred, sigma2_pred = gp.predict(x, eval_MSE=True) 
 
#Plots 
ax.scatter(X,y,s=400) #Sampled points 
ax.plot(x,y_pred) #Approximation 
ax.fill_between(x.ravel(),y_pred-10*sigma2_pred,y_pred+10*sigma2_pred,color='black',alpha=0.1) #Confidence intervals 
ax.plot(x_real,y_real) #True function 

请您参考如下方法:

您需要对 sigma2_pred 求平方根,因为那是 MSE,或均值 平方 误差。置信区间应基于其平方根,如下所示:

#GP call 
gp = gaussian_process.GaussianProcess() 
gp.fit(X, y)   
y_pred, sigma2_pred = gp.predict(x, eval_MSE=True) 
sd_pred = np.sqrt(sigma2_pred) 
 
#Plots 
ax.scatter(X,y,s=400) #Sampled points 
ax.plot(x,y_pred) #Approximation 
ax.fill_between(x.ravel(),y_pred-10*sd_pred,y_pred+10*sd_pred,color='black',alpha=0.1) #Confidence intervals 
ax.plot(x_real,y_real) #True function 

参见 here对于 scikit-learn 文档页面上的示例。它们还取平方根。