.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/mlearning/regression_model/plot_polynomial_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_polynomial_regression.py: Polynomial regression ===================== A :class:`.PolynomialRegressor` is a polynomial regression model based on a :class:`.LinearRegressor`. This design choice was made because a polynomial regression model is a generalized linear model whose basis functions are monomials. Thus, a :class:`.PolynomialRegressor` benefits from the same settings as :class:`.LinearRegressor`: offset can be set to zero and regularization techniques can be used. .. seealso:: You will find more information about these settings in :ref:`the example about the linear regression model `. .. GENERATED FROM PYTHON SOURCE LINES 39-50 .. code-block:: Python from __future__ import annotations from matplotlib import pyplot as plt from numpy import array 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 51-56 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 56-61 .. code-block:: Python discipline = create_discipline( "AnalyticDiscipline", name="f", expressions={"y": "(6*x-2)**2*sin(12*x-4)"}, ) .. GENERATED FROM PYTHON SOURCE LINES 62-63 and seek to approximate it over the input space .. GENERATED FROM PYTHON SOURCE LINES 63-66 .. 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 67-69 To do this, we create a training dataset with 6 equispaced points: .. GENERATED FROM PYTHON SOURCE LINES 69-73 .. 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:22: *** Start Sampling execution *** INFO - 16:22:22: Sampling INFO - 16:22:22: Disciplines: f INFO - 16:22:22: MDO formulation: MDF INFO - 16:22:22: Running the algorithm PYDOE_FULLFACT: INFO - 16:22:22: 17%|█▋ | 1/6 [00:00<00:00, 671.84 it/sec] INFO - 16:22:22: 33%|███▎ | 2/6 [00:00<00:00, 1094.83 it/sec] INFO - 16:22:22: 50%|█████ | 3/6 [00:00<00:00, 1432.81 it/sec] INFO - 16:22:22: 67%|██████▋ | 4/6 [00:00<00:00, 1692.62 it/sec] INFO - 16:22:22: 83%|████████▎ | 5/6 [00:00<00:00, 1918.19 it/sec] INFO - 16:22:22: 100%|██████████| 6/6 [00:00<00:00, 2053.85 it/sec] INFO - 16:22:22: *** End Sampling execution *** .. GENERATED FROM PYTHON SOURCE LINES 74-81 Basics ------ Training ~~~~~~~~ Then, we train a polynomial regression model with a degree of 2 (default) from these samples: .. GENERATED FROM PYTHON SOURCE LINES 81-84 .. code-block:: Python model = create_regression_model("PolynomialRegressor", training_dataset) model.learn() .. GENERATED FROM PYTHON SOURCE LINES 85-89 Prediction ~~~~~~~~~~ Once it is built, we can predict the output value of :math:`f` at a new input point: .. GENERATED FROM PYTHON SOURCE LINES 89-93 .. 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.90980781])} .. GENERATED FROM PYTHON SOURCE LINES 94-95 as well as its Jacobian value: .. GENERATED FROM PYTHON SOURCE LINES 95-98 .. code-block:: Python jacobian_value = model.predict_jacobian(input_value) jacobian_value .. rst-class:: sphx-glr-script-out .. code-block:: none {'y': {'x': array([[20.65451891]])}} .. GENERATED FROM PYTHON SOURCE LINES 99-103 Plotting ~~~~~~~~ Of course, you can see that the quadratic model is no good at all here: .. GENERATED FROM PYTHON SOURCE LINES 103-115 .. 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_polynomial_regression_001.png :alt: plot polynomial regression :srcset: /examples/mlearning/regression_model/images/sphx_glr_plot_polynomial_regression_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none INFO - 16:22:22: *** Start Sampling execution *** INFO - 16:22:22: Sampling INFO - 16:22:22: Disciplines: f INFO - 16:22:22: MDO formulation: MDF INFO - 16:22:22: Running the algorithm PYDOE_FULLFACT: INFO - 16:22:22: 1%| | 1/100 [00:00<00:00, 4396.55 it/sec] INFO - 16:22:22: 2%|▏ | 2/100 [00:00<00:00, 3981.30 it/sec] INFO - 16:22:22: 3%|▎ | 3/100 [00:00<00:00, 4008.57 it/sec] INFO - 16:22:22: 4%|▍ | 4/100 [00:00<00:00, 3970.00 it/sec] INFO - 16:22:22: 5%|▌ | 5/100 [00:00<00:00, 4003.73 it/sec] INFO - 16:22:22: 6%|▌ | 6/100 [00:00<00:00, 4051.16 it/sec] INFO - 16:22:22: 7%|▋ | 7/100 [00:00<00:00, 4101.15 it/sec] INFO - 16:22:22: 8%|▊ | 8/100 [00:00<00:00, 4104.02 it/sec] INFO - 16:22:22: 9%|▉ | 9/100 [00:00<00:00, 4130.96 it/sec] INFO - 16:22:22: 10%|█ | 10/100 [00:00<00:00, 4163.91 it/sec] INFO - 16:22:22: 11%|█ | 11/100 [00:00<00:00, 4178.35 it/sec] INFO - 16:22:22: 12%|█▏ | 12/100 [00:00<00:00, 4208.68 it/sec] INFO - 16:22:22: 13%|█▎ | 13/100 [00:00<00:00, 4192.05 it/sec] INFO - 16:22:22: 14%|█▍ | 14/100 [00:00<00:00, 4214.17 it/sec] INFO - 16:22:22: 15%|█▌ | 15/100 [00:00<00:00, 4238.67 it/sec] INFO - 16:22:22: 16%|█▌ | 16/100 [00:00<00:00, 4260.61 it/sec] INFO - 16:22:22: 17%|█▋ | 17/100 [00:00<00:00, 4261.74 it/sec] INFO - 16:22:22: 18%|█▊ | 18/100 [00:00<00:00, 4273.12 it/sec] INFO - 16:22:22: 19%|█▉ | 19/100 [00:00<00:00, 4288.65 it/sec] INFO - 16:22:22: 20%|██ | 20/100 [00:00<00:00, 4304.28 it/sec] INFO - 16:22:22: 21%|██ | 21/100 [00:00<00:00, 4319.36 it/sec] INFO - 16:22:22: 22%|██▏ | 22/100 [00:00<00:00, 4305.66 it/sec] INFO - 16:22:22: 23%|██▎ | 23/100 [00:00<00:00, 4314.55 it/sec] INFO - 16:22:22: 24%|██▍ | 24/100 [00:00<00:00, 4327.93 it/sec] INFO - 16:22:22: 25%|██▌ | 25/100 [00:00<00:00, 4338.70 it/sec] INFO - 16:22:22: 26%|██▌ | 26/100 [00:00<00:00, 4335.03 it/sec] INFO - 16:22:22: 27%|██▋ | 27/100 [00:00<00:00, 4341.60 it/sec] INFO - 16:22:22: 28%|██▊ | 28/100 [00:00<00:00, 4350.13 it/sec] INFO - 16:22:22: 29%|██▉ | 29/100 [00:00<00:00, 4350.47 it/sec] INFO - 16:22:22: 30%|███ | 30/100 [00:00<00:00, 4359.83 it/sec] INFO - 16:22:22: 31%|███ | 31/100 [00:00<00:00, 4354.58 it/sec] INFO - 16:22:22: 32%|███▏ | 32/100 [00:00<00:00, 4363.24 it/sec] INFO - 16:22:22: 33%|███▎ | 33/100 [00:00<00:00, 4371.69 it/sec] INFO - 16:22:22: 34%|███▍ | 34/100 [00:00<00:00, 4379.53 it/sec] INFO - 16:22:22: 35%|███▌ | 35/100 [00:00<00:00, 4376.88 it/sec] INFO - 16:22:22: 36%|███▌ | 36/100 [00:00<00:00, 4381.36 it/sec] INFO - 16:22:22: 37%|███▋ | 37/100 [00:00<00:00, 4388.59 it/sec] INFO - 16:22:22: 38%|███▊ | 38/100 [00:00<00:00, 4394.97 it/sec] INFO - 16:22:22: 39%|███▉ | 39/100 [00:00<00:00, 4401.87 it/sec] INFO - 16:22:22: 40%|████ | 40/100 [00:00<00:00, 4397.24 it/sec] INFO - 16:22:22: 41%|████ | 41/100 [00:00<00:00, 4402.40 it/sec] INFO - 16:22:22: 42%|████▏ | 42/100 [00:00<00:00, 4407.32 it/sec] INFO - 16:22:22: 43%|████▎ | 43/100 [00:00<00:00, 4412.57 it/sec] INFO - 16:22:22: 44%|████▍ | 44/100 [00:00<00:00, 4409.68 it/sec] INFO - 16:22:22: 45%|████▌ | 45/100 [00:00<00:00, 4413.20 it/sec] INFO - 16:22:22: 46%|████▌ | 46/100 [00:00<00:00, 4418.09 it/sec] INFO - 16:22:22: 47%|████▋ | 47/100 [00:00<00:00, 4418.72 it/sec] INFO - 16:22:22: 48%|████▊ | 48/100 [00:00<00:00, 4423.98 it/sec] INFO - 16:22:22: 49%|████▉ | 49/100 [00:00<00:00, 4419.42 it/sec] INFO - 16:22:22: 50%|█████ | 50/100 [00:00<00:00, 4422.79 it/sec] INFO - 16:22:22: 51%|█████ | 51/100 [00:00<00:00, 4427.58 it/sec] INFO - 16:22:22: 52%|█████▏ | 52/100 [00:00<00:00, 4432.10 it/sec] INFO - 16:22:22: 53%|█████▎ | 53/100 [00:00<00:00, 4428.78 it/sec] INFO - 16:22:22: 54%|█████▍ | 54/100 [00:00<00:00, 4429.91 it/sec] INFO - 16:22:22: 55%|█████▌ | 55/100 [00:00<00:00, 4433.81 it/sec] INFO - 16:22:22: 56%|█████▌ | 56/100 [00:00<00:00, 4438.17 it/sec] INFO - 16:22:22: 57%|█████▋ | 57/100 [00:00<00:00, 4442.29 it/sec] INFO - 16:22:22: 58%|█████▊ | 58/100 [00:00<00:00, 4435.67 it/sec] INFO - 16:22:22: 59%|█████▉ | 59/100 [00:00<00:00, 4438.34 it/sec] INFO - 16:22:22: 60%|██████ | 60/100 [00:00<00:00, 4441.47 it/sec] INFO - 16:22:22: 61%|██████ | 61/100 [00:00<00:00, 4413.00 it/sec] INFO - 16:22:22: 62%|██████▏ | 62/100 [00:00<00:00, 4407.80 it/sec] INFO - 16:22:22: 63%|██████▎ | 63/100 [00:00<00:00, 4408.28 it/sec] INFO - 16:22:22: 64%|██████▍ | 64/100 [00:00<00:00, 4408.31 it/sec] INFO - 16:22:22: 65%|██████▌ | 65/100 [00:00<00:00, 4409.91 it/sec] INFO - 16:22:22: 66%|██████▌ | 66/100 [00:00<00:00, 4406.97 it/sec] INFO - 16:22:22: 67%|██████▋ | 67/100 [00:00<00:00, 4408.55 it/sec] INFO - 16:22:22: 68%|██████▊ | 68/100 [00:00<00:00, 4411.03 it/sec] INFO - 16:22:22: 69%|██████▉ | 69/100 [00:00<00:00, 4413.24 it/sec] INFO - 16:22:22: 70%|███████ | 70/100 [00:00<00:00, 4416.72 it/sec] INFO - 16:22:22: 71%|███████ | 71/100 [00:00<00:00, 4413.75 it/sec] INFO - 16:22:22: 72%|███████▏ | 72/100 [00:00<00:00, 4416.35 it/sec] INFO - 16:22:22: 73%|███████▎ | 73/100 [00:00<00:00, 4418.94 it/sec] INFO - 16:22:22: 74%|███████▍ | 74/100 [00:00<00:00, 4422.04 it/sec] INFO - 16:22:22: 75%|███████▌ | 75/100 [00:00<00:00, 4419.77 it/sec] INFO - 16:22:22: 76%|███████▌ | 76/100 [00:00<00:00, 4420.69 it/sec] INFO - 16:22:22: 77%|███████▋ | 77/100 [00:00<00:00, 4423.28 it/sec] INFO - 16:22:22: 78%|███████▊ | 78/100 [00:00<00:00, 4426.05 it/sec] INFO - 16:22:22: 79%|███████▉ | 79/100 [00:00<00:00, 4428.39 it/sec] INFO - 16:22:22: 80%|████████ | 80/100 [00:00<00:00, 4425.95 it/sec] INFO - 16:22:22: 81%|████████ | 81/100 [00:00<00:00, 4426.45 it/sec] INFO - 16:22:22: 82%|████████▏ | 82/100 [00:00<00:00, 4426.82 it/sec] INFO - 16:22:22: 83%|████████▎ | 83/100 [00:00<00:00, 4427.86 it/sec] INFO - 16:22:22: 84%|████████▍ | 84/100 [00:00<00:00, 4425.98 it/sec] INFO - 16:22:22: 85%|████████▌ | 85/100 [00:00<00:00, 4426.68 it/sec] INFO - 16:22:22: 86%|████████▌ | 86/100 [00:00<00:00, 4427.30 it/sec] INFO - 16:22:22: 87%|████████▋ | 87/100 [00:00<00:00, 4428.99 it/sec] INFO - 16:22:22: 88%|████████▊ | 88/100 [00:00<00:00, 4431.22 it/sec] INFO - 16:22:22: 89%|████████▉ | 89/100 [00:00<00:00, 4428.36 it/sec] INFO - 16:22:22: 90%|█████████ | 90/100 [00:00<00:00, 4430.24 it/sec] INFO - 16:22:22: 91%|█████████ | 91/100 [00:00<00:00, 4432.70 it/sec] INFO - 16:22:22: 92%|█████████▏| 92/100 [00:00<00:00, 4434.80 it/sec] INFO - 16:22:22: 93%|█████████▎| 93/100 [00:00<00:00, 4433.02 it/sec] INFO - 16:22:22: 94%|█████████▍| 94/100 [00:00<00:00, 4433.73 it/sec] INFO - 16:22:22: 95%|█████████▌| 95/100 [00:00<00:00, 4435.80 it/sec] INFO - 16:22:22: 96%|█████████▌| 96/100 [00:00<00:00, 4438.32 it/sec] INFO - 16:22:22: 97%|█████████▋| 97/100 [00:00<00:00, 4440.40 it/sec] INFO - 16:22:22: 98%|█████████▊| 98/100 [00:00<00:00, 4438.42 it/sec] INFO - 16:22:22: 99%|█████████▉| 99/100 [00:00<00:00, 4439.84 it/sec] INFO - 16:22:22: 100%|██████████| 100/100 [00:00<00:00, 4387.76 it/sec] INFO - 16:22:22: *** End Sampling execution *** .. GENERATED FROM PYTHON SOURCE LINES 116-127 Settings -------- The :class:`.PolynomialRegressor` has many options defined in the :class:`.PolynomialRegressor_Settings` Pydantic model. Most of them are presented in :ref:`the example about the linear regression model `. The only one we will look at here is the degree of the polynomial regression model. This information can be set with the ``degree`` keyword. For example, we can use a cubic regression model instead of a quadratic one: .. GENERATED FROM PYTHON SOURCE LINES 127-129 .. code-block:: Python model = create_regression_model("PolynomialRegressor", training_dataset, degree=3) model.learn() .. GENERATED FROM PYTHON SOURCE LINES 130-131 and see that this model seems to be better: .. GENERATED FROM PYTHON SOURCE LINES 131-138 .. 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_polynomial_regression_002.png :alt: plot polynomial regression :srcset: /examples/mlearning/regression_model/images/sphx_glr_plot_polynomial_regression_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.151 seconds) .. _sphx_glr_download_examples_mlearning_regression_model_plot_polynomial_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_polynomial_regression.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_polynomial_regression.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_polynomial_regression.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_