.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/mlearning/regression_model/plot_gp_regression.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. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_mlearning_regression_model_plot_gp_regression.py: Gaussian process (GP) regression ================================ A :class:`.GaussianProcessRegressor` is a GP regression model based on `scikit-learn `__. .. seealso:: You can find more information about building GP models with scikit-learn on `this page `__. .. GENERATED FROM PYTHON SOURCE LINES 32-45 .. code-block:: Python from __future__ import annotations from matplotlib import pyplot as plt from numpy import array from sklearn.gaussian_process.kernels import RBF from sklearn.gaussian_process.kernels import Matern from gemseo import create_design_space from gemseo import create_discipline from gemseo import sample_disciplines from gemseo.mlearning import create_regression_model .. GENERATED FROM PYTHON SOURCE LINES 46-51 Problem ------- In this example, we represent the function :math:`f(x)=(6x-2)^2\sin(12x-4)` :cite:`forrester2008` by the :class:`.AnalyticDiscipline` .. GENERATED FROM PYTHON SOURCE LINES 51-56 .. code-block:: Python discipline = create_discipline( "AnalyticDiscipline", name="f", expressions={"y": "(6*x-2)**2*sin(12*x-4)"}, ) .. GENERATED FROM PYTHON SOURCE LINES 57-58 and seek to approximate it over the input space .. GENERATED FROM PYTHON SOURCE LINES 58-61 .. code-block:: Python input_space = create_design_space() input_space.add_variable("x", lower_bound=0.0, upper_bound=1.0) .. GENERATED FROM PYTHON SOURCE LINES 62-64 To do this, we create a training dataset with 6 equispaced points: .. GENERATED FROM PYTHON SOURCE LINES 64-68 .. code-block:: Python training_dataset = sample_disciplines( [discipline], input_space, "y", algo_name="PYDOE_FULLFACT", n_samples=6 ) .. rst-class:: sphx-glr-script-out .. code-block:: none INFO - 16:22:18: *** Start Sampling execution *** INFO - 16:22:18: Sampling INFO - 16:22:18: Disciplines: f INFO - 16:22:18: MDO formulation: MDF INFO - 16:22:18: Running the algorithm PYDOE_FULLFACT: INFO - 16:22:18: 17%|█▋ | 1/6 [00:00<00:00, 592.25 it/sec] INFO - 16:22:18: 33%|███▎ | 2/6 [00:00<00:00, 952.28 it/sec] INFO - 16:22:18: 50%|█████ | 3/6 [00:00<00:00, 1229.16 it/sec] INFO - 16:22:18: 67%|██████▋ | 4/6 [00:00<00:00, 1457.87 it/sec] INFO - 16:22:18: 83%|████████▎ | 5/6 [00:00<00:00, 1641.61 it/sec] INFO - 16:22:18: 100%|██████████| 6/6 [00:00<00:00, 1746.66 it/sec] INFO - 16:22:18: *** End Sampling execution *** .. GENERATED FROM PYTHON SOURCE LINES 69-75 Basics ------ Training ~~~~~~~~ Then, we train a GP regression model from these samples: .. GENERATED FROM PYTHON SOURCE LINES 75-78 .. code-block:: Python model = create_regression_model("GaussianProcessRegressor", training_dataset) model.learn() .. GENERATED FROM PYTHON SOURCE LINES 79-83 Prediction ~~~~~~~~~~ Once it is built, we can predict the output value of :math:`f` at a new input point: .. GENERATED FROM PYTHON SOURCE LINES 83-87 .. code-block:: Python input_value = {"x": array([0.65])} output_value = model.predict(input_value) output_value .. rst-class:: sphx-glr-script-out .. code-block:: none {'y': array([2.20380214])} .. GENERATED FROM PYTHON SOURCE LINES 88-89 but cannot predict its Jacobian value: .. GENERATED FROM PYTHON SOURCE LINES 89-94 .. code-block:: Python try: model.predict_jacobian(input_value) except NotImplementedError: print("The derivatives are not available for GaussianProcessRegressor.") .. rst-class:: sphx-glr-script-out .. code-block:: none The derivatives are not available for GaussianProcessRegressor. .. GENERATED FROM PYTHON SOURCE LINES 95-105 Uncertainty ~~~~~~~~~~~ GP models are often valued for their ability to provide model uncertainty. Indeed, a GP model is a random process fully characterized by its mean function and a covariance structure. Given an input point :math:`x`, the prediction is equal to the mean at :math:`x` and the uncertainty is equal to the standard deviation at :math:`x`: .. GENERATED FROM PYTHON SOURCE LINES 105-108 .. code-block:: Python standard_deviation = model.predict_std(input_value) standard_deviation .. rst-class:: sphx-glr-script-out .. code-block:: none array([[0.3140468]]) .. GENERATED FROM PYTHON SOURCE LINES 109-115 Plotting ~~~~~~~~ You can see that the GP model interpolates the training points but is very bad elsewhere. This case-dependent problem is due to poor auto-tuning of these length scales. We will look at how to correct this next. .. GENERATED FROM PYTHON SOURCE LINES 115-127 .. code-block:: Python test_dataset = sample_disciplines( [discipline], input_space, "y", algo_name="PYDOE_FULLFACT", n_samples=100 ) input_data = test_dataset.get_view(variable_names=model.input_names).to_numpy() reference_output_data = test_dataset.get_view(variable_names="y").to_numpy().ravel() predicted_output_data = model.predict(input_data).ravel() plt.plot(input_data.ravel(), reference_output_data, label="Reference") plt.plot(input_data.ravel(), predicted_output_data, label="Regression - Basics") plt.grid() plt.legend() plt.show() .. image-sg:: /examples/mlearning/regression_model/images/sphx_glr_plot_gp_regression_001.png :alt: plot gp regression :srcset: /examples/mlearning/regression_model/images/sphx_glr_plot_gp_regression_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none INFO - 16:22:18: *** Start Sampling execution *** INFO - 16:22:18: Sampling INFO - 16:22:18: Disciplines: f INFO - 16:22:18: MDO formulation: MDF INFO - 16:22:18: Running the algorithm PYDOE_FULLFACT: INFO - 16:22:18: 1%| | 1/100 [00:00<00:00, 4271.19 it/sec] INFO - 16:22:18: 2%|▏ | 2/100 [00:00<00:00, 3445.01 it/sec] INFO - 16:22:18: 3%|▎ | 3/100 [00:00<00:00, 3362.62 it/sec] INFO - 16:22:18: 4%|▍ | 4/100 [00:00<00:00, 3386.60 it/sec] INFO - 16:22:18: 5%|▌ | 5/100 [00:00<00:00, 3334.64 it/sec] INFO - 16:22:18: 6%|▌ | 6/100 [00:00<00:00, 3356.34 it/sec] INFO - 16:22:18: 7%|▋ | 7/100 [00:00<00:00, 3382.89 it/sec] INFO - 16:22:18: 8%|▊ | 8/100 [00:00<00:00, 3386.94 it/sec] INFO - 16:22:18: 9%|▉ | 9/100 [00:00<00:00, 3397.73 it/sec] INFO - 16:22:18: 10%|█ | 10/100 [00:00<00:00, 3410.84 it/sec] INFO - 16:22:18: 11%|█ | 11/100 [00:00<00:00, 3424.18 it/sec] INFO - 16:22:18: 12%|█▏ | 12/100 [00:00<00:00, 3421.83 it/sec] INFO - 16:22:18: 13%|█▎ | 13/100 [00:00<00:00, 3436.65 it/sec] INFO - 16:22:18: 14%|█▍ | 14/100 [00:00<00:00, 3453.73 it/sec] INFO - 16:22:18: 15%|█▌ | 15/100 [00:00<00:00, 3451.53 it/sec] INFO - 16:22:18: 16%|█▌ | 16/100 [00:00<00:00, 3461.00 it/sec] INFO - 16:22:18: 17%|█▋ | 17/100 [00:00<00:00, 3463.50 it/sec] INFO - 16:22:18: 18%|█▊ | 18/100 [00:00<00:00, 3474.02 it/sec] INFO - 16:22:18: 19%|█▉ | 19/100 [00:00<00:00, 3473.47 it/sec] INFO - 16:22:18: 20%|██ | 20/100 [00:00<00:00, 3483.93 it/sec] INFO - 16:22:18: 21%|██ | 21/100 [00:00<00:00, 3489.58 it/sec] INFO - 16:22:18: 22%|██▏ | 22/100 [00:00<00:00, 3497.64 it/sec] INFO - 16:22:18: 23%|██▎ | 23/100 [00:00<00:00, 3496.14 it/sec] INFO - 16:22:18: 24%|██▍ | 24/100 [00:00<00:00, 3505.23 it/sec] INFO - 16:22:18: 25%|██▌ | 25/100 [00:00<00:00, 3511.41 it/sec] INFO - 16:22:18: 26%|██▌ | 26/100 [00:00<00:00, 3508.52 it/sec] INFO - 16:22:18: 27%|██▋ | 27/100 [00:00<00:00, 3511.40 it/sec] INFO - 16:22:18: 28%|██▊ | 28/100 [00:00<00:00, 3514.39 it/sec] INFO - 16:22:18: 29%|██▉ | 29/100 [00:00<00:00, 3519.63 it/sec] INFO - 16:22:18: 30%|███ | 30/100 [00:00<00:00, 3518.61 it/sec] INFO - 16:22:18: 31%|███ | 31/100 [00:00<00:00, 3539.40 it/sec] INFO - 16:22:18: 32%|███▏ | 32/100 [00:00<00:00, 3559.96 it/sec] INFO - 16:22:18: 33%|███▎ | 33/100 [00:00<00:00, 3583.76 it/sec] INFO - 16:22:18: 34%|███▍ | 34/100 [00:00<00:00, 3600.62 it/sec] INFO - 16:22:18: 35%|███▌ | 35/100 [00:00<00:00, 3621.76 it/sec] INFO - 16:22:18: 36%|███▌ | 36/100 [00:00<00:00, 3643.00 it/sec] INFO - 16:22:18: 37%|███▋ | 37/100 [00:00<00:00, 3663.93 it/sec] INFO - 16:22:18: 38%|███▊ | 38/100 [00:00<00:00, 3678.87 it/sec] INFO - 16:22:18: 39%|███▉ | 39/100 [00:00<00:00, 3696.09 it/sec] INFO - 16:22:18: 40%|████ | 40/100 [00:00<00:00, 3714.32 it/sec] INFO - 16:22:18: 41%|████ | 41/100 [00:00<00:00, 3700.03 it/sec] INFO - 16:22:18: 42%|████▏ | 42/100 [00:00<00:00, 3710.13 it/sec] INFO - 16:22:18: 43%|████▎ | 43/100 [00:00<00:00, 3723.34 it/sec] INFO - 16:22:18: 44%|████▍ | 44/100 [00:00<00:00, 3738.16 it/sec] INFO - 16:22:18: 45%|████▌ | 45/100 [00:00<00:00, 3754.15 it/sec] INFO - 16:22:18: 46%|████▌ | 46/100 [00:00<00:00, 3769.28 it/sec] INFO - 16:22:18: 47%|████▋ | 47/100 [00:00<00:00, 3778.29 it/sec] INFO - 16:22:18: 48%|████▊ | 48/100 [00:00<00:00, 3791.18 it/sec] INFO - 16:22:18: 49%|████▉ | 49/100 [00:00<00:00, 3802.91 it/sec] INFO - 16:22:18: 50%|█████ | 50/100 [00:00<00:00, 3816.96 it/sec] INFO - 16:22:18: 51%|█████ | 51/100 [00:00<00:00, 3827.13 it/sec] INFO - 16:22:18: 52%|█████▏ | 52/100 [00:00<00:00, 3839.11 it/sec] INFO - 16:22:18: 53%|█████▎ | 53/100 [00:00<00:00, 3851.79 it/sec] INFO - 16:22:18: 54%|█████▍ | 54/100 [00:00<00:00, 3864.14 it/sec] INFO - 16:22:18: 55%|█████▌ | 55/100 [00:00<00:00, 3876.70 it/sec] INFO - 16:22:18: 56%|█████▌ | 56/100 [00:00<00:00, 3883.55 it/sec] INFO - 16:22:18: 57%|█████▋ | 57/100 [00:00<00:00, 3894.43 it/sec] INFO - 16:22:18: 58%|█████▊ | 58/100 [00:00<00:00, 3905.81 it/sec] INFO - 16:22:18: 59%|█████▉ | 59/100 [00:00<00:00, 3917.30 it/sec] INFO - 16:22:18: 60%|██████ | 60/100 [00:00<00:00, 3928.60 it/sec] INFO - 16:22:18: 61%|██████ | 61/100 [00:00<00:00, 3932.68 it/sec] INFO - 16:22:18: 62%|██████▏ | 62/100 [00:00<00:00, 3942.02 it/sec] INFO - 16:22:18: 63%|██████▎ | 63/100 [00:00<00:00, 3951.74 it/sec] INFO - 16:22:18: 64%|██████▍ | 64/100 [00:00<00:00, 3960.92 it/sec] INFO - 16:22:18: 65%|██████▌ | 65/100 [00:00<00:00, 3965.87 it/sec] INFO - 16:22:18: 66%|██████▌ | 66/100 [00:00<00:00, 3973.99 it/sec] INFO - 16:22:18: 67%|██████▋ | 67/100 [00:00<00:00, 3982.01 it/sec] INFO - 16:22:18: 68%|██████▊ | 68/100 [00:00<00:00, 3988.37 it/sec] INFO - 16:22:18: 69%|██████▉ | 69/100 [00:00<00:00, 3997.39 it/sec] INFO - 16:22:18: 70%|███████ | 70/100 [00:00<00:00, 4000.40 it/sec] INFO - 16:22:18: 71%|███████ | 71/100 [00:00<00:00, 4008.66 it/sec] INFO - 16:22:18: 72%|███████▏ | 72/100 [00:00<00:00, 4017.00 it/sec] INFO - 16:22:18: 73%|███████▎ | 73/100 [00:00<00:00, 4024.24 it/sec] INFO - 16:22:18: 74%|███████▍ | 74/100 [00:00<00:00, 4027.86 it/sec] INFO - 16:22:18: 75%|███████▌ | 75/100 [00:00<00:00, 4033.86 it/sec] INFO - 16:22:18: 76%|███████▌ | 76/100 [00:00<00:00, 4041.32 it/sec] INFO - 16:22:18: 77%|███████▋ | 77/100 [00:00<00:00, 4048.25 it/sec] INFO - 16:22:18: 78%|███████▊ | 78/100 [00:00<00:00, 4055.43 it/sec] INFO - 16:22:18: 79%|███████▉ | 79/100 [00:00<00:00, 4057.68 it/sec] INFO - 16:22:18: 80%|████████ | 80/100 [00:00<00:00, 4063.56 it/sec] INFO - 16:22:18: 81%|████████ | 81/100 [00:00<00:00, 4070.19 it/sec] INFO - 16:22:18: 82%|████████▏ | 82/100 [00:00<00:00, 4077.26 it/sec] INFO - 16:22:18: 83%|████████▎ | 83/100 [00:00<00:00, 4080.73 it/sec] INFO - 16:22:18: 84%|████████▍ | 84/100 [00:00<00:00, 4085.17 it/sec] INFO - 16:22:18: 85%|████████▌ | 85/100 [00:00<00:00, 4091.44 it/sec] INFO - 16:22:18: 86%|████████▌ | 86/100 [00:00<00:00, 4095.67 it/sec] INFO - 16:22:18: 87%|████████▋ | 87/100 [00:00<00:00, 4101.29 it/sec] INFO - 16:22:18: 88%|████████▊ | 88/100 [00:00<00:00, 4102.33 it/sec] INFO - 16:22:18: 89%|████████▉ | 89/100 [00:00<00:00, 4106.81 it/sec] INFO - 16:22:18: 90%|█████████ | 90/100 [00:00<00:00, 4112.87 it/sec] INFO - 16:22:18: 91%|█████████ | 91/100 [00:00<00:00, 4118.36 it/sec] INFO - 16:22:18: 92%|█████████▏| 92/100 [00:00<00:00, 4121.15 it/sec] INFO - 16:22:18: 93%|█████████▎| 93/100 [00:00<00:00, 4124.37 it/sec] INFO - 16:22:18: 94%|█████████▍| 94/100 [00:00<00:00, 4129.38 it/sec] INFO - 16:22:18: 95%|█████████▌| 95/100 [00:00<00:00, 4134.51 it/sec] INFO - 16:22:18: 96%|█████████▌| 96/100 [00:00<00:00, 4138.86 it/sec] INFO - 16:22:18: 97%|█████████▋| 97/100 [00:00<00:00, 4139.76 it/sec] INFO - 16:22:18: 98%|█████████▊| 98/100 [00:00<00:00, 4143.11 it/sec] INFO - 16:22:18: 99%|█████████▉| 99/100 [00:00<00:00, 4147.67 it/sec] INFO - 16:22:18: 100%|██████████| 100/100 [00:00<00:00, 4106.15 it/sec] INFO - 16:22:18: *** End Sampling execution *** .. GENERATED FROM PYTHON SOURCE LINES 128-145 Settings -------- The :class:`.GaussianProcessRegressor` has many options defined in the :class:`.GaussianProcessRegressor_Settings` Pydantic model. Here are the main ones. Kernel ~~~~~~ The ``kernel`` option defines the kernel function parametrizing the Gaussian process regressor and must be passed as a scikit-learn object. The default kernel is the Matérn 5/2 covariance function with input length scales belonging to the interval :math:`[0.01,100]`, initialized at 1 and optimized by the L-BFGS-B algorithm. We can replace this kernel by the Matérn 5/2 kernel with input length scales fixed at 1: .. GENERATED FROM PYTHON SOURCE LINES 145-152 .. code-block:: Python model = create_regression_model( "GaussianProcessRegressor", training_dataset, kernel=Matern(length_scale=1.0, length_scale_bounds="fixed", nu=2.5), ) model.learn() predicted_output_data_1 = model.predict(input_data).ravel() .. GENERATED FROM PYTHON SOURCE LINES 153-155 or a squared exponential covariance kernel with input length scales fixed at 1: .. GENERATED FROM PYTHON SOURCE LINES 155-162 .. code-block:: Python model = create_regression_model( "GaussianProcessRegressor", training_dataset, kernel=RBF(length_scale=1.0, length_scale_bounds="fixed"), ) model.learn() predicted_output_data_2 = model.predict(input_data).ravel() .. GENERATED FROM PYTHON SOURCE LINES 163-167 These two models are much better than the previous one, notably the one with the Matérn 5/2 kernel, which highlights that the concern with the initial model is the value of the length scales found by numerical optimization: .. GENERATED FROM PYTHON SOURCE LINES 167-176 .. code-block:: Python plt.plot(input_data.ravel(), reference_output_data, label="Reference") plt.plot(input_data.ravel(), predicted_output_data, label="Regression - Basics") plt.plot( input_data.ravel(), predicted_output_data_1, label="Regression - Kernel(Matern 2.5)" ) plt.plot(input_data.ravel(), predicted_output_data_2, label="Regression - Kernel(RBF)") plt.grid() plt.legend() plt.show() .. image-sg:: /examples/mlearning/regression_model/images/sphx_glr_plot_gp_regression_002.png :alt: plot gp regression :srcset: /examples/mlearning/regression_model/images/sphx_glr_plot_gp_regression_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 177-180 Bounds ~~~~~~ The ``bounds`` option defines the bounds of the input length scales; .. GENERATED FROM PYTHON SOURCE LINES 180-184 .. code-block:: Python model = create_regression_model( "GaussianProcessRegressor", training_dataset, bounds=(1e-1, 1e2) ) model.learn() .. GENERATED FROM PYTHON SOURCE LINES 185-186 Increasing the lower bounds can facilitate the training as in this example: .. GENERATED FROM PYTHON SOURCE LINES 186-194 .. code-block:: Python predicted_output_data_ = model.predict(input_data).ravel() plt.plot(input_data.ravel(), reference_output_data, label="Reference") plt.plot(input_data.ravel(), predicted_output_data, label="Regression - Basics") plt.plot(input_data.ravel(), predicted_output_data_, label="Regression - Bounds") plt.grid() plt.legend() plt.show() .. image-sg:: /examples/mlearning/regression_model/images/sphx_glr_plot_gp_regression_003.png :alt: plot gp regression :srcset: /examples/mlearning/regression_model/images/sphx_glr_plot_gp_regression_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 195-206 Alpha ~~~~~ The ``alpha`` parameter (default: 1e-10), often called *nugget effect*, is the value added to the diagonal of the training kernel matrix to avoid overfitting. When ``alpha`` is equal to zero, the GP model interpolates the training points at which the standard deviation is equal to zero. The larger ``alpha`` is, the less interpolating the GP model is. For example, we can increase the value to 0.1: .. GENERATED FROM PYTHON SOURCE LINES 206-211 .. code-block:: Python predicted_output_data_1 = predicted_output_data_ model = create_regression_model( "GaussianProcessRegressor", training_dataset, bounds=(1e-1, 1e2), alpha=0.1 ) model.learn() .. GENERATED FROM PYTHON SOURCE LINES 212-213 and see that the model moves away from the training points: .. GENERATED FROM PYTHON SOURCE LINES 213-220 .. code-block:: Python predicted_output_data_2 = model.predict(input_data).ravel() plt.plot(input_data.ravel(), reference_output_data, label="Reference") plt.plot(input_data.ravel(), predicted_output_data_1, label="Regression - Alpha(1e-10)") plt.plot(input_data.ravel(), predicted_output_data_2, label="Regression - Alpha(1e-1)") plt.grid() plt.legend() plt.show() .. image-sg:: /examples/mlearning/regression_model/images/sphx_glr_plot_gp_regression_004.png :alt: plot gp regression :srcset: /examples/mlearning/regression_model/images/sphx_glr_plot_gp_regression_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.293 seconds) .. _sphx_glr_download_examples_mlearning_regression_model_plot_gp_regression.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_gp_regression.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_gp_regression.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_gp_regression.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_