【问题标题】:scikit-learn / Gaussian Process is not scale invariantscikit-learn / 高斯过程不是尺度不变的
【发布时间】:2016-09-20 22:02:47
【问题描述】:

我正在使用库 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 的平方根。这是平均 squared 误差,所以如果你直接使用它,它不会线性缩放。我将在下面发布答案。

标签: python scikit-learn gaussian


【解决方案1】:

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

#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

有关 scikit-learn 文档页面上的示例,请参见 here。它们也取平方根。

【讨论】:

    猜你喜欢
    • 2016-04-15
    • 2020-02-24
    • 2020-03-11
    • 2018-10-15
    • 2019-09-15
    • 2019-07-26
    • 2020-08-16
    • 2020-11-11
    相关资源
    最近更新 更多