.. 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-42 .. code-block:: Python from __future__ import annotations from matplotlib import pyplot as plt from numpy import array from gemseo import configure_logger from gemseo import create_discipline from gemseo import create_parameter_space from gemseo import sample_disciplines from gemseo.mlearning import create_regression_model configure_logger() .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 43-48 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 48-53 .. code-block:: Python discipline = create_discipline( "AnalyticDiscipline", name="f", expressions={"y": "(6*x-2)**2*sin(12*x-4)"}, ) .. GENERATED FROM PYTHON SOURCE LINES 54-55 and seek to approximate it over the input space .. GENERATED FROM PYTHON SOURCE LINES 55-58 .. code-block:: Python input_space = create_parameter_space() input_space.add_random_variable("x", "OTUniformDistribution") .. GENERATED FROM PYTHON SOURCE LINES 59-61 To do this, we create a training dataset with 6 equispaced points: .. GENERATED FROM PYTHON SOURCE LINES 61-65 .. 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 WARNING - 20:35:11: No coupling in MDA, switching chain_linearize to True. INFO - 20:35:11: *** Start Sampling execution *** INFO - 20:35:11: Sampling INFO - 20:35:11: Disciplines: f INFO - 20:35:11: MDO formulation: MDF INFO - 20:35:11: Running the algorithm PYDOE_FULLFACT: INFO - 20:35:11: 10%|█ | 1/10 [00:00<00:00, 661.67 it/sec] INFO - 20:35:11: 20%|██ | 2/10 [00:00<00:00, 1086.47 it/sec] INFO - 20:35:11: 30%|███ | 3/10 [00:00<00:00, 1423.25 it/sec] INFO - 20:35:11: 40%|████ | 4/10 [00:00<00:00, 1711.96 it/sec] INFO - 20:35:11: 50%|█████ | 5/10 [00:00<00:00, 1960.69 it/sec] INFO - 20:35:11: 60%|██████ | 6/10 [00:00<00:00, 2174.34 it/sec] INFO - 20:35:11: 70%|███████ | 7/10 [00:00<00:00, 2360.90 it/sec] INFO - 20:35:11: 80%|████████ | 8/10 [00:00<00:00, 2505.00 it/sec] INFO - 20:35:11: 90%|█████████ | 9/10 [00:00<00:00, 2648.85 it/sec] INFO - 20:35:11: 100%|██████████| 10/10 [00:00<00:00, 2780.45 it/sec] INFO - 20:35:11: *** End Sampling execution *** .. GENERATED FROM PYTHON SOURCE LINES 66-72 Basics ------ Training ~~~~~~~~ Then, we train an PCE regression model from these samples: .. GENERATED FROM PYTHON SOURCE LINES 72-75 .. code-block:: Python model = create_regression_model("PCERegressor", training_dataset) model.learn() .. rst-class:: sphx-glr-script-out .. code-block:: none WARNING - 20:35:11: Remove input data transformation because PCERegressor does not support transformers. .. GENERATED FROM PYTHON SOURCE LINES 76-80 Prediction ~~~~~~~~~~ Once it is built, we can predict the output value of :math:`f` at a new input point: .. GENERATED FROM PYTHON SOURCE LINES 80-84 .. 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 85-86 as well as its Jacobian value: .. GENERATED FROM PYTHON SOURCE LINES 86-89 .. 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 90-94 Plotting ~~~~~~~~ Of course, you can see that the quadratic model is no good at all here: .. GENERATED FROM PYTHON SOURCE LINES 94-106 .. 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 WARNING - 20:35:11: No coupling in MDA, switching chain_linearize to True. INFO - 20:35:11: *** Start Sampling execution *** INFO - 20:35:11: Sampling INFO - 20:35:11: Disciplines: f INFO - 20:35:11: MDO formulation: MDF INFO - 20:35:11: Running the algorithm PYDOE_FULLFACT: INFO - 20:35:11: 1%| | 1/100 [00:00<00:00, 3446.43 it/sec] INFO - 20:35:11: 2%|▏ | 2/100 [00:00<00:00, 3425.32 it/sec] INFO - 20:35:11: 3%|▎ | 3/100 [00:00<00:00, 3680.29 it/sec] INFO - 20:35:11: 4%|▍ | 4/100 [00:00<00:00, 3884.51 it/sec] INFO - 20:35:11: 5%|▌ | 5/100 [00:00<00:00, 4028.34 it/sec] INFO - 20:35:11: 6%|▌ | 6/100 [00:00<00:00, 4060.31 it/sec] INFO - 20:35:11: 7%|▋ | 7/100 [00:00<00:00, 4137.56 it/sec] INFO - 20:35:11: 8%|▊ | 8/100 [00:00<00:00, 4220.68 it/sec] INFO - 20:35:11: 9%|▉ | 9/100 [00:00<00:00, 4281.36 it/sec] INFO - 20:35:11: 10%|█ | 10/100 [00:00<00:00, 4346.43 it/sec] INFO - 20:35:11: 11%|█ | 11/100 [00:00<00:00, 4365.76 it/sec] INFO - 20:35:11: 12%|█▏ | 12/100 [00:00<00:00, 4407.71 it/sec] INFO - 20:35:11: 13%|█▎ | 13/100 [00:00<00:00, 4450.37 it/sec] INFO - 20:35:11: 14%|█▍ | 14/100 [00:00<00:00, 4488.97 it/sec] INFO - 20:35:11: 15%|█▌ | 15/100 [00:00<00:00, 4516.16 it/sec] INFO - 20:35:11: 16%|█▌ | 16/100 [00:00<00:00, 4521.55 it/sec] INFO - 20:35:11: 17%|█▋ | 17/100 [00:00<00:00, 4550.01 it/sec] INFO - 20:35:11: 18%|█▊ | 18/100 [00:00<00:00, 4576.44 it/sec] INFO - 20:35:11: 19%|█▉ | 19/100 [00:00<00:00, 4604.33 it/sec] INFO - 20:35:11: 20%|██ | 20/100 [00:00<00:00, 4631.78 it/sec] INFO - 20:35:11: 21%|██ | 21/100 [00:00<00:00, 4616.86 it/sec] INFO - 20:35:11: 22%|██▏ | 22/100 [00:00<00:00, 4630.64 it/sec] INFO - 20:35:11: 23%|██▎ | 23/100 [00:00<00:00, 4649.33 it/sec] INFO - 20:35:11: 24%|██▍ | 24/100 [00:00<00:00, 4668.12 it/sec] INFO - 20:35:11: 25%|██▌ | 25/100 [00:00<00:00, 4686.37 it/sec] INFO - 20:35:11: 26%|██▌ | 26/100 [00:00<00:00, 4669.32 it/sec] INFO - 20:35:11: 27%|██▋ | 27/100 [00:00<00:00, 4678.44 it/sec] INFO - 20:35:11: 28%|██▊ | 28/100 [00:00<00:00, 4691.43 it/sec] INFO - 20:35:11: 29%|██▉ | 29/100 [00:00<00:00, 4705.41 it/sec] INFO - 20:35:11: 30%|███ | 30/100 [00:00<00:00, 4720.48 it/sec] INFO - 20:35:11: 31%|███ | 31/100 [00:00<00:00, 4714.24 it/sec] INFO - 20:35:11: 32%|███▏ | 32/100 [00:00<00:00, 4722.82 it/sec] INFO - 20:35:11: 33%|███▎ | 33/100 [00:00<00:00, 4734.30 it/sec] INFO - 20:35:11: 34%|███▍ | 34/100 [00:00<00:00, 4743.43 it/sec] INFO - 20:35:11: 35%|███▌ | 35/100 [00:00<00:00, 4751.29 it/sec] INFO - 20:35:11: 36%|███▌ | 36/100 [00:00<00:00, 4745.58 it/sec] INFO - 20:35:11: 37%|███▋ | 37/100 [00:00<00:00, 4755.01 it/sec] INFO - 20:35:11: 38%|███▊ | 38/100 [00:00<00:00, 4765.54 it/sec] INFO - 20:35:11: 39%|███▉ | 39/100 [00:00<00:00, 4775.58 it/sec] INFO - 20:35:11: 40%|████ | 40/100 [00:00<00:00, 4785.97 it/sec] INFO - 20:35:11: 41%|████ | 41/100 [00:00<00:00, 4780.70 it/sec] INFO - 20:35:11: 42%|████▏ | 42/100 [00:00<00:00, 4788.02 it/sec] INFO - 20:35:11: 43%|████▎ | 43/100 [00:00<00:00, 4797.44 it/sec] INFO - 20:35:11: 44%|████▍ | 44/100 [00:00<00:00, 4805.60 it/sec] INFO - 20:35:11: 45%|████▌ | 45/100 [00:00<00:00, 4813.42 it/sec] INFO - 20:35:11: 46%|████▌ | 46/100 [00:00<00:00, 4804.95 it/sec] INFO - 20:35:11: 47%|████▋ | 47/100 [00:00<00:00, 4811.98 it/sec] INFO - 20:35:11: 48%|████▊ | 48/100 [00:00<00:00, 4820.12 it/sec] INFO - 20:35:11: 49%|████▉ | 49/100 [00:00<00:00, 4826.70 it/sec] INFO - 20:35:11: 50%|█████ | 50/100 [00:00<00:00, 4832.93 it/sec] INFO - 20:35:11: 51%|█████ | 51/100 [00:00<00:00, 4827.35 it/sec] INFO - 20:35:11: 52%|█████▏ | 52/100 [00:00<00:00, 4832.68 it/sec] INFO - 20:35:11: 53%|█████▎ | 53/100 [00:00<00:00, 4786.78 it/sec] INFO - 20:35:11: 54%|█████▍ | 54/100 [00:00<00:00, 4789.64 it/sec] INFO - 20:35:11: 55%|█████▌ | 55/100 [00:00<00:00, 4786.53 it/sec] INFO - 20:35:11: 56%|█████▌ | 56/100 [00:00<00:00, 4790.07 it/sec] INFO - 20:35:11: 57%|█████▋ | 57/100 [00:00<00:00, 4793.97 it/sec] INFO - 20:35:11: 58%|█████▊ | 58/100 [00:00<00:00, 4799.83 it/sec] INFO - 20:35:11: 59%|█████▉ | 59/100 [00:00<00:00, 4806.15 it/sec] INFO - 20:35:11: 60%|██████ | 60/100 [00:00<00:00, 4805.94 it/sec] INFO - 20:35:11: 61%|██████ | 61/100 [00:00<00:00, 4807.90 it/sec] INFO - 20:35:11: 62%|██████▏ | 62/100 [00:00<00:00, 4811.49 it/sec] INFO - 20:35:11: 63%|██████▎ | 63/100 [00:00<00:00, 4816.29 it/sec] INFO - 20:35:11: 64%|██████▍ | 64/100 [00:00<00:00, 4820.69 it/sec] INFO - 20:35:11: 65%|██████▌ | 65/100 [00:00<00:00, 4816.78 it/sec] INFO - 20:35:11: 66%|██████▌ | 66/100 [00:00<00:00, 4819.78 it/sec] INFO - 20:35:11: 67%|██████▋ | 67/100 [00:00<00:00, 4825.26 it/sec] INFO - 20:35:11: 68%|██████▊ | 68/100 [00:00<00:00, 4831.33 it/sec] INFO - 20:35:11: 69%|██████▉ | 69/100 [00:00<00:00, 4837.15 it/sec] INFO - 20:35:11: 70%|███████ | 70/100 [00:00<00:00, 4837.00 it/sec] INFO - 20:35:11: 71%|███████ | 71/100 [00:00<00:00, 4838.98 it/sec] INFO - 20:35:11: 72%|███████▏ | 72/100 [00:00<00:00, 4840.75 it/sec] INFO - 20:35:11: 73%|███████▎ | 73/100 [00:00<00:00, 4844.15 it/sec] INFO - 20:35:11: 74%|███████▍ | 74/100 [00:00<00:00, 4848.91 it/sec] INFO - 20:35:11: 75%|███████▌ | 75/100 [00:00<00:00, 4847.41 it/sec] INFO - 20:35:11: 76%|███████▌ | 76/100 [00:00<00:00, 4848.91 it/sec] INFO - 20:35:11: 77%|███████▋ | 77/100 [00:00<00:00, 4852.48 it/sec] INFO - 20:35:11: 78%|███████▊ | 78/100 [00:00<00:00, 4857.11 it/sec] INFO - 20:35:11: 79%|███████▉ | 79/100 [00:00<00:00, 4861.14 it/sec] INFO - 20:35:11: 80%|████████ | 80/100 [00:00<00:00, 4860.21 it/sec] INFO - 20:35:11: 81%|████████ | 81/100 [00:00<00:00, 4862.02 it/sec] INFO - 20:35:11: 82%|████████▏ | 82/100 [00:00<00:00, 4864.82 it/sec] INFO - 20:35:11: 83%|████████▎ | 83/100 [00:00<00:00, 4867.75 it/sec] INFO - 20:35:11: 84%|████████▍ | 84/100 [00:00<00:00, 4871.23 it/sec] INFO - 20:35:11: 85%|████████▌ | 85/100 [00:00<00:00, 4867.77 it/sec] INFO - 20:35:11: 86%|████████▌ | 86/100 [00:00<00:00, 4868.47 it/sec] INFO - 20:35:11: 87%|████████▋ | 87/100 [00:00<00:00, 4870.98 it/sec] INFO - 20:35:11: 88%|████████▊ | 88/100 [00:00<00:00, 4873.88 it/sec] INFO - 20:35:11: 89%|████████▉ | 89/100 [00:00<00:00, 4876.84 it/sec] INFO - 20:35:11: 90%|█████████ | 90/100 [00:00<00:00, 4875.65 it/sec] INFO - 20:35:11: 91%|█████████ | 91/100 [00:00<00:00, 4876.29 it/sec] INFO - 20:35:11: 92%|█████████▏| 92/100 [00:00<00:00, 4879.38 it/sec] INFO - 20:35:11: 93%|█████████▎| 93/100 [00:00<00:00, 4882.29 it/sec] INFO - 20:35:11: 94%|█████████▍| 94/100 [00:00<00:00, 4885.20 it/sec] INFO - 20:35:11: 95%|█████████▌| 95/100 [00:00<00:00, 4883.49 it/sec] INFO - 20:35:11: 96%|█████████▌| 96/100 [00:00<00:00, 4885.38 it/sec] INFO - 20:35:11: 97%|█████████▋| 97/100 [00:00<00:00, 4889.11 it/sec] INFO - 20:35:11: 98%|█████████▊| 98/100 [00:00<00:00, 4892.54 it/sec] INFO - 20:35:11: 99%|█████████▉| 99/100 [00:00<00:00, 4895.84 it/sec] INFO - 20:35:11: 100%|██████████| 100/100 [00:00<00:00, 4895.31 it/sec] INFO - 20:35:11: *** End Sampling execution *** .. GENERATED FROM PYTHON SOURCE LINES 107-114 Settings -------- The :class:`.PCERegressor` has many options defined in the :class:`.PCERegressor_Settings` Pydantic model. Degree ~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 114-116 .. code-block:: Python model = create_regression_model("PCERegressor", training_dataset, degree=3) model.learn() .. rst-class:: sphx-glr-script-out .. code-block:: none WARNING - 20:35:11: Remove input data transformation because PCERegressor does not support transformers. .. GENERATED FROM PYTHON SOURCE LINES 117-118 and see that this model seems to be better: .. GENERATED FROM PYTHON SOURCE LINES 118-125 .. 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.198 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 `_