.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/mlearning/regression_model/plot_pce_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_pce_regression.py: Polynomial chaos expansion (PCE) ================================ A :class:`.PCERegressor` is a PCE model based on `OpenTURNS `__. .. GENERATED FROM PYTHON SOURCE LINES 28-39 .. code-block:: Python from __future__ import annotations from matplotlib import pyplot as plt from numpy import array from gemseo import create_discipline from gemseo import create_parameter_space from gemseo import sample_disciplines from gemseo.mlearning import create_regression_model .. GENERATED FROM PYTHON SOURCE LINES 40-45 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 45-50 .. code-block:: Python discipline = create_discipline( "AnalyticDiscipline", name="f", expressions={"y": "(6*x-2)**2*sin(12*x-4)"}, ) .. GENERATED FROM PYTHON SOURCE LINES 51-52 and seek to approximate it over the input space .. GENERATED FROM PYTHON SOURCE LINES 52-55 .. code-block:: Python input_space = create_parameter_space() input_space.add_random_variable("x", "OTUniformDistribution") .. GENERATED FROM PYTHON SOURCE LINES 56-58 To do this, we create a training dataset with 6 equispaced points: .. GENERATED FROM PYTHON SOURCE LINES 58-62 .. code-block:: Python training_dataset = sample_disciplines( [discipline], input_space, "y", algo_name="PYDOE_FULLFACT", n_samples=10 ) .. rst-class:: sphx-glr-script-out .. code-block:: none INFO - 16:22:21: *** Start Sampling execution *** INFO - 16:22:21: Sampling INFO - 16:22:21: Disciplines: f INFO - 16:22:21: MDO formulation: MDF INFO - 16:22:21: Running the algorithm PYDOE_FULLFACT: INFO - 16:22:21: 10%|█ | 1/10 [00:00<00:00, 711.99 it/sec] INFO - 16:22:21: 20%|██ | 2/10 [00:00<00:00, 1161.21 it/sec] INFO - 16:22:21: 30%|███ | 3/10 [00:00<00:00, 1494.76 it/sec] INFO - 16:22:21: 40%|████ | 4/10 [00:00<00:00, 1771.99 it/sec] INFO - 16:22:21: 50%|█████ | 5/10 [00:00<00:00, 1994.44 it/sec] INFO - 16:22:21: 60%|██████ | 6/10 [00:00<00:00, 2187.57 it/sec] INFO - 16:22:21: 70%|███████ | 7/10 [00:00<00:00, 2337.78 it/sec] INFO - 16:22:21: 80%|████████ | 8/10 [00:00<00:00, 2480.00 it/sec] INFO - 16:22:21: 90%|█████████ | 9/10 [00:00<00:00, 2608.76 it/sec] INFO - 16:22:21: 100%|██████████| 10/10 [00:00<00:00, 2630.65 it/sec] INFO - 16:22:21: *** End Sampling execution *** .. GENERATED FROM PYTHON SOURCE LINES 63-69 Basics ------ Training ~~~~~~~~ Then, we train an PCE regression model from these samples: .. GENERATED FROM PYTHON SOURCE LINES 69-72 .. code-block:: Python model = create_regression_model("PCERegressor", training_dataset) model.learn() .. rst-class:: sphx-glr-script-out .. code-block:: none WARNING - 16:22:21: Remove input data transformation because PCERegressor does not support transformers. .. GENERATED FROM PYTHON SOURCE LINES 73-77 Prediction ~~~~~~~~~~ Once it is built, we can predict the output value of :math:`f` at a new input point: .. GENERATED FROM PYTHON SOURCE LINES 77-81 .. 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([-0.81106394])} .. GENERATED FROM PYTHON SOURCE LINES 82-83 as well as its Jacobian value: .. GENERATED FROM PYTHON SOURCE LINES 83-86 .. code-block:: Python jacobian_value = model.predict_jacobian(input_value) jacobian_value .. rst-class:: sphx-glr-script-out .. code-block:: none {'y': {'x': array([[18.2279622]])}} .. GENERATED FROM PYTHON SOURCE LINES 87-91 Plotting ~~~~~~~~ Of course, you can see that the quadratic model is no good at all here: .. GENERATED FROM PYTHON SOURCE LINES 91-103 .. 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_pce_regression_001.png :alt: plot pce regression :srcset: /examples/mlearning/regression_model/images/sphx_glr_plot_pce_regression_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none INFO - 16:22:21: *** Start Sampling execution *** INFO - 16:22:21: Sampling INFO - 16:22:21: Disciplines: f INFO - 16:22:21: MDO formulation: MDF INFO - 16:22:21: Running the algorithm PYDOE_FULLFACT: INFO - 16:22:21: 1%| | 1/100 [00:00<00:00, 4198.50 it/sec] INFO - 16:22:21: 2%|▏ | 2/100 [00:00<00:00, 3479.31 it/sec] INFO - 16:22:21: 3%|▎ | 3/100 [00:00<00:00, 3416.48 it/sec] INFO - 16:22:21: 4%|▍ | 4/100 [00:00<00:00, 3519.45 it/sec] INFO - 16:22:21: 5%|▌ | 5/100 [00:00<00:00, 3580.59 it/sec] INFO - 16:22:21: 6%|▌ | 6/100 [00:00<00:00, 3611.11 it/sec] INFO - 16:22:21: 7%|▋ | 7/100 [00:00<00:00, 3645.86 it/sec] INFO - 16:22:21: 8%|▊ | 8/100 [00:00<00:00, 3690.14 it/sec] INFO - 16:22:21: 9%|▉ | 9/100 [00:00<00:00, 3763.21 it/sec] INFO - 16:22:21: 10%|█ | 10/100 [00:00<00:00, 3830.76 it/sec] INFO - 16:22:21: 11%|█ | 11/100 [00:00<00:00, 3847.34 it/sec] INFO - 16:22:21: 12%|█▏ | 12/100 [00:00<00:00, 3892.32 it/sec] INFO - 16:22:21: 13%|█▎ | 13/100 [00:00<00:00, 3919.63 it/sec] INFO - 16:22:21: 14%|█▍ | 14/100 [00:00<00:00, 3957.42 it/sec] INFO - 16:22:21: 15%|█▌ | 15/100 [00:00<00:00, 3973.38 it/sec] INFO - 16:22:21: 16%|█▌ | 16/100 [00:00<00:00, 4001.01 it/sec] INFO - 16:22:21: 17%|█▋ | 17/100 [00:00<00:00, 4030.25 it/sec] INFO - 16:22:21: 18%|█▊ | 18/100 [00:00<00:00, 4059.00 it/sec] INFO - 16:22:21: 19%|█▉ | 19/100 [00:00<00:00, 4068.81 it/sec] INFO - 16:22:21: 20%|██ | 20/100 [00:00<00:00, 4084.83 it/sec] INFO - 16:22:21: 21%|██ | 21/100 [00:00<00:00, 4102.87 it/sec] INFO - 16:22:21: 22%|██▏ | 22/100 [00:00<00:00, 4113.90 it/sec] INFO - 16:22:21: 23%|██▎ | 23/100 [00:00<00:00, 4130.37 it/sec] INFO - 16:22:21: 24%|██▍ | 24/100 [00:00<00:00, 4134.19 it/sec] INFO - 16:22:21: 25%|██▌ | 25/100 [00:00<00:00, 4150.15 it/sec] INFO - 16:22:21: 26%|██▌ | 26/100 [00:00<00:00, 4166.74 it/sec] INFO - 16:22:21: 27%|██▋ | 27/100 [00:00<00:00, 4182.53 it/sec] INFO - 16:22:21: 28%|██▊ | 28/100 [00:00<00:00, 4183.70 it/sec] INFO - 16:22:21: 29%|██▉ | 29/100 [00:00<00:00, 4191.85 it/sec] INFO - 16:22:21: 30%|███ | 30/100 [00:00<00:00, 4203.97 it/sec] INFO - 16:22:21: 31%|███ | 31/100 [00:00<00:00, 4209.10 it/sec] INFO - 16:22:21: 32%|███▏ | 32/100 [00:00<00:00, 4221.61 it/sec] INFO - 16:22:21: 33%|███▎ | 33/100 [00:00<00:00, 4219.75 it/sec] INFO - 16:22:21: 34%|███▍ | 34/100 [00:00<00:00, 4231.39 it/sec] INFO - 16:22:21: 35%|███▌ | 35/100 [00:00<00:00, 4243.16 it/sec] INFO - 16:22:21: 36%|███▌ | 36/100 [00:00<00:00, 4254.22 it/sec] INFO - 16:22:21: 37%|███▋ | 37/100 [00:00<00:00, 4257.01 it/sec] INFO - 16:22:21: 38%|███▊ | 38/100 [00:00<00:00, 4265.47 it/sec] INFO - 16:22:21: 39%|███▉ | 39/100 [00:00<00:00, 4276.21 it/sec] INFO - 16:22:21: 40%|████ | 40/100 [00:00<00:00, 4286.79 it/sec] INFO - 16:22:21: 41%|████ | 41/100 [00:00<00:00, 4297.23 it/sec] INFO - 16:22:21: 42%|████▏ | 42/100 [00:00<00:00, 4279.28 it/sec] INFO - 16:22:21: 43%|████▎ | 43/100 [00:00<00:00, 4286.00 it/sec] INFO - 16:22:21: 44%|████▍ | 44/100 [00:00<00:00, 4294.54 it/sec] INFO - 16:22:21: 45%|████▌ | 45/100 [00:00<00:00, 4303.32 it/sec] INFO - 16:22:21: 46%|████▌ | 46/100 [00:00<00:00, 4303.67 it/sec] INFO - 16:22:21: 47%|████▋ | 47/100 [00:00<00:00, 4309.19 it/sec] INFO - 16:22:21: 48%|████▊ | 48/100 [00:00<00:00, 4316.98 it/sec] INFO - 16:22:21: 49%|████▉ | 49/100 [00:00<00:00, 4318.48 it/sec] INFO - 16:22:21: 50%|█████ | 50/100 [00:00<00:00, 4323.85 it/sec] INFO - 16:22:21: 51%|█████ | 51/100 [00:00<00:00, 4319.66 it/sec] INFO - 16:22:21: 52%|█████▏ | 52/100 [00:00<00:00, 4293.30 it/sec] INFO - 16:22:21: 53%|█████▎ | 53/100 [00:00<00:00, 4295.70 it/sec] INFO - 16:22:21: 54%|█████▍ | 54/100 [00:00<00:00, 4300.95 it/sec] INFO - 16:22:21: 55%|█████▌ | 55/100 [00:00<00:00, 4299.44 it/sec] INFO - 16:22:21: 56%|█████▌ | 56/100 [00:00<00:00, 4304.77 it/sec] INFO - 16:22:21: 57%|█████▋ | 57/100 [00:00<00:00, 4310.54 it/sec] INFO - 16:22:21: 58%|█████▊ | 58/100 [00:00<00:00, 4315.43 it/sec] INFO - 16:22:21: 59%|█████▉ | 59/100 [00:00<00:00, 4314.53 it/sec] INFO - 16:22:21: 60%|██████ | 60/100 [00:00<00:00, 4317.42 it/sec] INFO - 16:22:21: 61%|██████ | 61/100 [00:00<00:00, 4321.18 it/sec] INFO - 16:22:21: 62%|██████▏ | 62/100 [00:00<00:00, 4326.25 it/sec] INFO - 16:22:21: 63%|██████▎ | 63/100 [00:00<00:00, 4331.54 it/sec] INFO - 16:22:21: 64%|██████▍ | 64/100 [00:00<00:00, 4330.79 it/sec] INFO - 16:22:21: 65%|██████▌ | 65/100 [00:00<00:00, 4335.03 it/sec] INFO - 16:22:21: 66%|██████▌ | 66/100 [00:00<00:00, 4338.19 it/sec] INFO - 16:22:21: 67%|██████▋ | 67/100 [00:00<00:00, 4341.46 it/sec] INFO - 16:22:21: 68%|██████▊ | 68/100 [00:00<00:00, 4334.93 it/sec] INFO - 16:22:21: 69%|██████▉ | 69/100 [00:00<00:00, 4335.75 it/sec] INFO - 16:22:21: 70%|███████ | 70/100 [00:00<00:00, 4340.52 it/sec] INFO - 16:22:21: 71%|███████ | 71/100 [00:00<00:00, 4345.16 it/sec] INFO - 16:22:21: 72%|███████▏ | 72/100 [00:00<00:00, 4350.00 it/sec] INFO - 16:22:21: 73%|███████▎ | 73/100 [00:00<00:00, 4348.16 it/sec] INFO - 16:22:21: 74%|███████▍ | 74/100 [00:00<00:00, 4352.77 it/sec] INFO - 16:22:21: 75%|███████▌ | 75/100 [00:00<00:00, 4357.93 it/sec] INFO - 16:22:21: 76%|███████▌ | 76/100 [00:00<00:00, 4362.73 it/sec] INFO - 16:22:21: 77%|███████▋ | 77/100 [00:00<00:00, 4363.40 it/sec] INFO - 16:22:21: 78%|███████▊ | 78/100 [00:00<00:00, 4365.80 it/sec] INFO - 16:22:21: 79%|███████▉ | 79/100 [00:00<00:00, 4370.39 it/sec] INFO - 16:22:21: 80%|████████ | 80/100 [00:00<00:00, 4375.28 it/sec] INFO - 16:22:21: 81%|████████ | 81/100 [00:00<00:00, 4379.60 it/sec] INFO - 16:22:21: 82%|████████▏ | 82/100 [00:00<00:00, 4379.41 it/sec] INFO - 16:22:21: 83%|████████▎ | 83/100 [00:00<00:00, 4380.72 it/sec] INFO - 16:22:21: 84%|████████▍ | 84/100 [00:00<00:00, 4383.85 it/sec] INFO - 16:22:21: 85%|████████▌ | 85/100 [00:00<00:00, 4385.08 it/sec] INFO - 16:22:21: 86%|████████▌ | 86/100 [00:00<00:00, 4388.31 it/sec] INFO - 16:22:21: 87%|████████▋ | 87/100 [00:00<00:00, 4386.08 it/sec] INFO - 16:22:21: 88%|████████▊ | 88/100 [00:00<00:00, 4389.12 it/sec] INFO - 16:22:21: 89%|████████▉ | 89/100 [00:00<00:00, 4392.20 it/sec] INFO - 16:22:21: 90%|█████████ | 90/100 [00:00<00:00, 4395.78 it/sec] INFO - 16:22:21: 91%|█████████ | 91/100 [00:00<00:00, 4395.23 it/sec] INFO - 16:22:21: 92%|█████████▏| 92/100 [00:00<00:00, 4397.50 it/sec] INFO - 16:22:21: 93%|█████████▎| 93/100 [00:00<00:00, 4400.41 it/sec] INFO - 16:22:21: 94%|█████████▍| 94/100 [00:00<00:00, 4402.44 it/sec] INFO - 16:22:21: 95%|█████████▌| 95/100 [00:00<00:00, 4405.78 it/sec] INFO - 16:22:21: 96%|█████████▌| 96/100 [00:00<00:00, 4404.38 it/sec] INFO - 16:22:21: 97%|█████████▋| 97/100 [00:00<00:00, 4406.55 it/sec] INFO - 16:22:21: 98%|█████████▊| 98/100 [00:00<00:00, 4408.14 it/sec] INFO - 16:22:21: 99%|█████████▉| 99/100 [00:00<00:00, 4410.93 it/sec] INFO - 16:22:21: 100%|██████████| 100/100 [00:00<00:00, 4363.48 it/sec] INFO - 16:22:21: *** End Sampling execution *** .. GENERATED FROM PYTHON SOURCE LINES 104-111 Settings -------- The :class:`.PCERegressor` has many options defined in the :class:`.PCERegressor_Settings` Pydantic model. Degree ~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 111-113 .. code-block:: Python model = create_regression_model("PCERegressor", training_dataset, degree=3) model.learn() .. rst-class:: sphx-glr-script-out .. code-block:: none WARNING - 16:22:22: Remove input data transformation because PCERegressor does not support transformers. .. GENERATED FROM PYTHON SOURCE LINES 114-115 and see that this model seems to be better: .. GENERATED FROM PYTHON SOURCE LINES 115-122 .. 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 - Degree(3)") plt.grid() plt.legend() plt.show() .. image-sg:: /examples/mlearning/regression_model/images/sphx_glr_plot_pce_regression_002.png :alt: plot pce regression :srcset: /examples/mlearning/regression_model/images/sphx_glr_plot_pce_regression_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.185 seconds) .. _sphx_glr_download_examples_mlearning_regression_model_plot_pce_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_pce_regression.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_pce_regression.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_pce_regression.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_