XOR数据集上的高斯过程分类(GPC)示例#

此示例演示了在 XOR 数据上的 GPC。比较了平稳、各向同性核 (RBF) 和非平稳核 (DotProduct)。在此特定数据集上,DotProduct 核取得了显著更好的结果,因为类别边界是线性的并与坐标轴重合。通常,平稳核常能取得更好的结果。

302**2 * RBF(length_scale=1.55)  Log-Marginal-Likelihood:-24.237, 316**2 * DotProduct(sigma_0=0.0104) ** 2  Log-Marginal-Likelihood:-9.284
/home/circleci/project/sklearn/gaussian_process/kernels.py:450: ConvergenceWarning:

The optimal value found for dimension 0 of parameter k1__constant_value is close to the specified upper bound 100000.0. Increasing the bound and calling fit again may find a better value.

# Authors: The scikit-learn developers
# SPDX-License-Identifier: BSD-3-Clause

import matplotlib.pyplot as plt
import numpy as np

from sklearn.gaussian_process import GaussianProcessClassifier
from sklearn.gaussian_process.kernels import RBF, DotProduct

xx, yy = np.meshgrid(np.linspace(-3, 3, 50), np.linspace(-3, 3, 50))
rng = np.random.RandomState(0)
X = rng.randn(200, 2)
Y = np.logical_xor(X[:, 0] > 0, X[:, 1] > 0)

# fit the model
plt.figure(figsize=(10, 5))
kernels = [1.0 * RBF(length_scale=1.15), 1.0 * DotProduct(sigma_0=1.0) ** 2]
for i, kernel in enumerate(kernels):
    clf = GaussianProcessClassifier(kernel=kernel, warm_start=True).fit(X, Y)

    # plot the decision function for each datapoint on the grid
    Z = clf.predict_proba(np.vstack((xx.ravel(), yy.ravel())).T)[:, 1]
    Z = Z.reshape(xx.shape)

    plt.subplot(1, 2, i + 1)
    image = plt.imshow(
        Z,
        interpolation="nearest",
        extent=(xx.min(), xx.max(), yy.min(), yy.max()),
        aspect="auto",
        origin="lower",
        cmap=plt.cm.PuOr_r,
    )
    contours = plt.contour(xx, yy, Z, levels=[0.5], linewidths=2, colors=["k"])
    plt.scatter(X[:, 0], X[:, 1], s=30, c=Y, cmap=plt.cm.Paired, edgecolors=(0, 0, 0))
    plt.xticks(())
    plt.yticks(())
    plt.axis([-3, 3, -3, 3])
    plt.colorbar(image)
    plt.title(
        "%s\n Log-Marginal-Likelihood:%.3f"
        % (clf.kernel_, clf.log_marginal_likelihood(clf.kernel_.theta)),
        fontsize=12,
    )

plt.tight_layout()
plt.show()

脚本总运行时间: (0 分钟 0.432 秒)

相关示例

鸢尾花数据集上的高斯过程分类 (GPC)

鸢尾花数据集上的高斯过程分类 (GPC)

SGD:加权样本

SGD:加权样本

多层感知机中正则化的变化

多层感知机中正则化的变化

SVM 间隔示例

SVM 间隔示例

由 Sphinx-Gallery 生成的图库