{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Illustration of Gaussian process classification (GPC) on the XOR dataset\n\nThis example illustrates GPC on XOR data. Compared are a stationary, isotropic\nkernel (RBF) and a non-stationary kernel (DotProduct). On this particular\ndataset, the DotProduct kernel obtains considerably better results because the\nclass-boundaries are linear and coincide with the coordinate axes. In general,\nstationary kernels often obtain better results.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Authors: The scikit-learn developers\n# SPDX-License-Identifier: BSD-3-Clause\n\nimport matplotlib.pyplot as plt\nimport numpy as np\n\nfrom sklearn.gaussian_process import GaussianProcessClassifier\nfrom sklearn.gaussian_process.kernels import RBF, DotProduct\n\nxx, yy = np.meshgrid(np.linspace(-3, 3, 50), np.linspace(-3, 3, 50))\nrng = np.random.RandomState(0)\nX = rng.randn(200, 2)\nY = np.logical_xor(X[:, 0] > 0, X[:, 1] > 0)\n\n# fit the model\nplt.figure(figsize=(10, 5))\nkernels = [1.0 * RBF(length_scale=1.15), 1.0 * DotProduct(sigma_0=1.0) ** 2]\nfor i, kernel in enumerate(kernels):\n clf = GaussianProcessClassifier(kernel=kernel, warm_start=True).fit(X, Y)\n\n # plot the decision function for each datapoint on the grid\n Z = clf.predict_proba(np.vstack((xx.ravel(), yy.ravel())).T)[:, 1]\n Z = Z.reshape(xx.shape)\n\n plt.subplot(1, 2, i + 1)\n image = plt.imshow(\n Z,\n interpolation=\"nearest\",\n extent=(xx.min(), xx.max(), yy.min(), yy.max()),\n aspect=\"auto\",\n origin=\"lower\",\n cmap=plt.cm.PuOr_r,\n )\n contours = plt.contour(xx, yy, Z, levels=[0.5], linewidths=2, colors=[\"k\"])\n plt.scatter(X[:, 0], X[:, 1], s=30, c=Y, cmap=plt.cm.Paired, edgecolors=(0, 0, 0))\n plt.xticks(())\n plt.yticks(())\n plt.axis([-3, 3, -3, 3])\n plt.colorbar(image)\n plt.title(\n \"%s\\n Log-Marginal-Likelihood:%.3f\"\n % (clf.kernel_, clf.log_marginal_likelihood(clf.kernel_.theta)),\n fontsize=12,\n )\n\nplt.tight_layout()\nplt.show()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.18" } }, "nbformat": 4, "nbformat_minor": 0 }