You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决Scikit-learn中高斯过程回归的ConvergenceWarning问题

问题描述

我使用Scikit-learn的高斯过程回归器(GPR)拟合周期性无均值趋势的数据,参考官方案例定义了不含长期趋势的复合核函数,代码如下:

from sklearn.gaussian_process.kernels import (RBF, ExpSineSquared, 
                                              RationalQuadratic, WhiteKernel, ConstantKernel)
from sklearn.gaussian_process import GaussianProcessRegressor as GPR
import numpy as np

# 建模周期性
seasonal_kernel = (
    2.0**2
    * RBF(length_scale=100.0, length_scale_bounds=(1e-2,1e7))
    * ExpSineSquared(length_scale=1.0, length_scale_bounds=(1e-2,1e7), 
                     periodicity=1.0, periodicity_bounds="fixed")
)

# 建模小幅度波动
irregularities_kernel = 0.5**2 * RationalQuadratic(length_scale=1.0, 
                                length_scale_bounds=(1e-2,1e7), alpha=1.0)

# 建模噪声
noise_kernel = 0.1**2 * RBF(length_scale=0.1, length_scale_bounds=(1e-2,1e7)) + \
    WhiteKernel(noise_level=0.1**2, noise_level_bounds=(1e-5, 1e5)
)

co2_kernel = (
    seasonal_kernel + irregularities_kernel + noise_kernel
)

随后用该核初始化回归器并循环拟合数据:

gpr = GPR(n_restarts_optimizer=10, kernel=co2_kernel, alpha=150, normalize_y=False)
for x,y in zip(x_list, y_list):
    gpr.fit(x,y)

拟合过程中出现大量ConvergenceWarning,示例如下:

C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\sklearn\gaussian_process\kernels.py:430: ConvergenceWarning: The optimal value found for dimension 0 of parameter k1__k2__k1__constant_value is close to the specified upper bound 100000.0. Increasing the bound and calling fit again may find a better value.
C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\sklearn\gaussian_process\kernels.py:430: ConvergenceWarning: The optimal value found for dimension 0 of parameter k2__k1__k1__constant_value is close to the specified upper bound 100000.0. Increasing the bound and calling fit again may find a better value.
C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\sklearn\gaussian_process\kernels.py:430: ConvergenceWarning: The optimal value found for dimension 0 of parameter k1__k2__k2__alpha is close to the specified upper bound 100000.0. Increasing the bound and calling fit again may find a better value.

我已为所有核添加length_scale_bounds解决了部分警告,但存在以下核心问题:

  • 无法将警告关联到复合核中的特定子核;
  • 不知如何修复alpha和constant_value相关的警告;
  • 目前GPR模型效果远差于SVR,需优先解决上述两个问题。

解决方案

1. 关联警告与复合核中的子核

Scikit-learn的复合核参数采用__分隔层级命名,要对应警告中的参数到具体子核,最直接的方法是打印核的完整参数映射:

在定义完co2_kernel后,运行以下代码:

for param_name, param_value in co2_kernel.get_params().items():
    print(f"{param_name}: {param_value}")

输出会显示每个参数名对应的子核和参数值,例如:

  • k2__k2__alpha 对应 irregularities_kernel 中的RationalQuadratic的alpha参数;
  • k1__k1__constant_value 对应 seasonal_kernel 中最外层的ConstantKernel(即2.0**2对应的缩放核);
  • k1__k2__k1__length_scale 对应 seasonal_kernel 中RBF的length_scale参数。

通过这种方式,你可以精准匹配警告中的参数名到对应的子核组件。

2. 修复alpha和constant_value相关的警告

警告的本质是优化器找到的最优参数接近你设置的边界,需针对性调整参数边界:

针对ConstantValue警告

你当前用数值直接乘核(如2.0**2 * RBF),Scikit-learn会自动将数值包装为ConstantKernel,其默认边界为(1e-5, 1e5)。若优化器需要更大/更小的值,就会触发警告。

解决方法:显式定义ConstantKernel并设置贴合数据尺度的边界,避免盲目设置过大范围(如1e7)导致优化效率下降:

# 替换seasonal_kernel中的2.0**2 * RBF
seasonal_kernel = (
    ConstantKernel(constant_value=2.0**2, constant_value_bounds=(1e-3, 1e3))
    * RBF(length_scale=100.0, length_scale_bounds=(1e-2, 1e3))
    * ExpSineSquared(length_scale=1.0, length_scale_bounds=(1e-2, 1e3), 
                     periodicity=1.0, periodicity_bounds="fixed")
)

# 替换irregularities_kernel中的0.5**2 * RationalQuadratic
irregularities_kernel = ConstantKernel(constant_value=0.5**2, constant_value_bounds=(1e-3, 1e3)) * RationalQuadratic(
    length_scale=1.0, length_scale_bounds=(1e-2, 1e3), 
    alpha=1.0, alpha_bounds=(1e-2, 1e3)
)

# 替换noise_kernel中的0.1**2 * RBF
noise_kernel = ConstantKernel(constant_value=0.1**2, constant_value_bounds=(1e-4, 1e2)) * RBF(
    length_scale=0.1, length_scale_bounds=(1e-2, 1e2)
) + WhiteKernel(noise_level=0.1**2, noise_level_bounds=(1e-5, 1e3))

边界设置建议:参考目标变量y的方差范围,ConstantKernel的constant_value控制核的振幅,应与y的方差匹配。

针对RationalQuadratic的alpha警告

alpha参数控制RationalQuadratic核的异方差性,默认边界(1e-5, 1e5)可能过窄。解决方法:根据数据调整alpha的边界,例如设为(1e-2, 1e3)(如上述代码所示),避免设置过大范围导致优化效率降低。


额外优化建议
  1. 移除GPR的alpha参数:你已在noise_kernel中使用WhiteKernel建模噪声,GPR的alpha参数会额外添加全局噪声,导致双重建模,建议删除alpha=150。
  2. 修正拟合逻辑:循环调用gpr.fit(x,y)会覆盖之前的拟合结果,最终仅保留最后一组数据的模型。若需拟合多组数据,应为每组数据创建独立的GPR实例。
  3. 数据标准化:GPR对数据尺度敏感,建议用StandardScaler对x做标准化,对y做标准化后设置normalize_y=True,可大幅提升优化收敛速度和模型效果。

内容的提问来源于stack exchange,提问作者Ferdinando Randisi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 22:17:01