.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/gaussian_process/plot_gpc_isoprobability.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_gaussian_process_plot_gpc_isoprobability.py: ================================================================= 高斯过程分类 (GPC) 的等概率线 ================================================================= 一个二维分类示例,展示了预测概率的等概率线。 .. GENERATED FROM PYTHON SOURCE LINES 9-97 .. image-sg:: /auto_examples/gaussian_process/images/sphx_glr_plot_gpc_isoprobability_001.png :alt: plot gpc isoprobability :srcset: /auto_examples/gaussian_process/images/sphx_glr_plot_gpc_isoprobability_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Learned kernel: 0.0256**2 * DotProduct(sigma_0=5.72) ** 2 | .. code-block:: Python # 作者:scikit-learn 开发者 # SPDX-License-Identifier: BSD-3-Clause import numpy as np from matplotlib import cm from matplotlib import pyplot as plt from sklearn.gaussian_process import GaussianProcessClassifier from sklearn.gaussian_process.kernels import ConstantKernel as C from sklearn.gaussian_process.kernels import DotProduct # 一些常数 lim = 8 def g(x): """用于预测的函数(分类将包括预测g(x)是否小于等于0) """ return 5.0 - x[:, 1] - 0.5 * x[:, 0] ** 2.0 # 实验设计 X = np.array( [ [-4.61611719, -6.00099547], [4.10469096, 5.32782448], [0.00000000, -0.50000000], [-6.17289014, -4.6984743], [1.3109306, -6.93271427], [-5.03823144, 3.10584743], [-2.87600388, 6.74310541], [5.21301203, 4.26386883], ] ) # 观察 y = np.array(g(X) > 0, dtype=int) # 实例化并拟合高斯过程模型 kernel = C(0.1, (1e-5, np.inf)) * DotProduct(sigma_0=0.1) ** 2 gp = GaussianProcessClassifier(kernel=kernel) gp.fit(X, y) print("Learned kernel: %s " % gp.kernel_) # 评估实际函数和预测概率 res = 50 x1, x2 = np.meshgrid(np.linspace(-lim, lim, res), np.linspace(-lim, lim, res)) xx = np.vstack([x1.reshape(x1.size), x2.reshape(x2.size)]).T y_true = g(xx) y_prob = gp.predict_proba(xx)[:, 1] y_true = y_true.reshape((res, res)) y_prob = y_prob.reshape((res, res)) # 绘制概率分类等值线 fig = plt.figure(1) ax = fig.gca() ax.axes.set_aspect("equal") plt.xticks([]) plt.yticks([]) ax.set_xticklabels([]) ax.set_yticklabels([]) plt.xlabel("$x_1$") plt.ylabel("$x_2$") cax = plt.imshow(y_prob, cmap=cm.gray_r, alpha=0.8, extent=(-lim, lim, -lim, lim)) norm = plt.matplotlib.colors.Normalize(vmin=0.0, vmax=0.9) cb = plt.colorbar(cax, ticks=[0.0, 0.2, 0.4, 0.6, 0.8, 1.0], norm=norm) cb.set_label(r"${\rm \mathbb{P}}\left[\widehat{G}(\mathbf{x}) \leq 0\right]$") plt.clim(0, 1) plt.plot(X[y <= 0, 0], X[y <= 0, 1], "r.", markersize=12) plt.plot(X[y > 0, 0], X[y > 0, 1], "b.", markersize=12) plt.contour(x1, x2, y_true, [0.0], colors="k", linestyles="dashdot") cs = plt.contour(x1, x2, y_prob, [0.666], colors="b", linestyles="solid") plt.clabel(cs, fontsize=11) cs = plt.contour(x1, x2, y_prob, [0.5], colors="k", linestyles="dashed") plt.clabel(cs, fontsize=11) cs = plt.contour(x1, x2, y_prob, [0.334], colors="r", linestyles="solid") plt.clabel(cs, fontsize=11) plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.074 seconds) .. _sphx_glr_download_auto_examples_gaussian_process_plot_gpc_isoprobability.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/scikit-learn/scikit-learn/main?urlpath=lab/tree/notebooks/auto_examples/gaussian_process/plot_gpc_isoprobability.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_gpc_isoprobability.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_gpc_isoprobability.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_gpc_isoprobability.zip ` .. include:: plot_gpc_isoprobability.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_