.. 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-53 .. 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_design_space from gemseo import create_discipline 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 54-59 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 59-64 .. code-block:: Python discipline = create_discipline( "AnalyticDiscipline", name="f", expressions={"y": "(6*x-2)**2*sin(12*x-4)"}, ) .. GENERATED FROM PYTHON SOURCE LINES 65-66 and seek to approximate it over the input space .. GENERATED FROM PYTHON SOURCE LINES 66-69 .. 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 70-72 To do this, we create a training dataset with 6 equispaced points: .. GENERATED FROM PYTHON SOURCE LINES 72-76 .. 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 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: 17%|█▋ | 1/6 [00:00<00:00, 600.73 it/sec] INFO - 20:35:11: 33%|███▎ | 2/6 [00:00<00:00, 974.29 it/sec] INFO - 20:35:11: 50%|█████ | 3/6 [00:00<00:00, 1287.65 it/sec] INFO - 20:35:11: 67%|██████▋ | 4/6 [00:00<00:00, 1556.33 it/sec] INFO - 20:35:11: 83%|████████▎ | 5/6 [00:00<00:00, 1770.79 it/sec] INFO - 20:35:11: 100%|██████████| 6/6 [00:00<00:00, 1973.64 it/sec] INFO - 20:35:11: *** End Sampling execution *** .. GENERATED FROM PYTHON SOURCE LINES 77-84 Basics ------ Training ~~~~~~~~ Then, we train a polynomial regression model with a degree of 2 (default) from these samples: .. GENERATED FROM PYTHON SOURCE LINES 84-87 .. code-block:: Python model = create_regression_model("PolynomialRegressor", training_dataset) model.learn() .. GENERATED FROM PYTHON SOURCE LINES 88-92 Prediction ~~~~~~~~~~ Once it is built, we can predict the output value of :math:`f` at a new input point: .. GENERATED FROM PYTHON SOURCE LINES 92-96 .. 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 97-98 as well as its Jacobian value: .. GENERATED FROM PYTHON SOURCE LINES 98-101 .. 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 102-106 Plotting ~~~~~~~~ Of course, you can see that the quadratic model is no good at all here: .. GENERATED FROM PYTHON SOURCE LINES 106-118 .. 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 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, 3196.88 it/sec] INFO - 20:35:11: 2%|▏ | 2/100 [00:00<00:00, 3080.65 it/sec] INFO - 20:35:11: 3%|▎ | 3/100 [00:00<00:00, 3306.94 it/sec] INFO - 20:35:11: 4%|▍ | 4/100 [00:00<00:00, 3524.63 it/sec] INFO - 20:35:11: 5%|▌ | 5/100 [00:00<00:00, 3703.91 it/sec] INFO - 20:35:11: 6%|▌ | 6/100 [00:00<00:00, 3791.18 it/sec] INFO - 20:35:11: 7%|▋ | 7/100 [00:00<00:00, 3883.61 it/sec] INFO - 20:35:11: 8%|▊ | 8/100 [00:00<00:00, 3972.82 it/sec] INFO - 20:35:11: 9%|▉ | 9/100 [00:00<00:00, 4054.64 it/sec] INFO - 20:35:11: 10%|█ | 10/100 [00:00<00:00, 4128.66 it/sec] INFO - 20:35:11: 11%|█ | 11/100 [00:00<00:00, 4140.85 it/sec] INFO - 20:35:11: 12%|█▏ | 12/100 [00:00<00:00, 4186.98 it/sec] INFO - 20:35:11: 13%|█▎ | 13/100 [00:00<00:00, 4235.35 it/sec] INFO - 20:35:11: 14%|█▍ | 14/100 [00:00<00:00, 4279.90 it/sec] INFO - 20:35:11: 15%|█▌ | 15/100 [00:00<00:00, 4319.87 it/sec] INFO - 20:35:11: 16%|█▌ | 16/100 [00:00<00:00, 4323.19 it/sec] INFO - 20:35:11: 17%|█▋ | 17/100 [00:00<00:00, 4358.65 it/sec] INFO - 20:35:11: 18%|█▊ | 18/100 [00:00<00:00, 4391.94 it/sec] INFO - 20:35:11: 19%|█▉ | 19/100 [00:00<00:00, 4420.94 it/sec] INFO - 20:35:11: 20%|██ | 20/100 [00:00<00:00, 4448.30 it/sec] INFO - 20:35:11: 21%|██ | 21/100 [00:00<00:00, 4438.42 it/sec] INFO - 20:35:11: 22%|██▏ | 22/100 [00:00<00:00, 4455.35 it/sec] INFO - 20:35:11: 23%|██▎ | 23/100 [00:00<00:00, 4474.86 it/sec] INFO - 20:35:11: 24%|██▍ | 24/100 [00:00<00:00, 4493.70 it/sec] INFO - 20:35:11: 25%|██▌ | 25/100 [00:00<00:00, 4499.17 it/sec] INFO - 20:35:11: 26%|██▌ | 26/100 [00:00<00:00, 4513.74 it/sec] INFO - 20:35:11: 27%|██▋ | 27/100 [00:00<00:00, 4533.11 it/sec] INFO - 20:35:11: 28%|██▊ | 28/100 [00:00<00:00, 4552.49 it/sec] INFO - 20:35:11: 29%|██▉ | 29/100 [00:00<00:00, 4569.99 it/sec] INFO - 20:35:11: 30%|███ | 30/100 [00:00<00:00, 4567.63 it/sec] INFO - 20:35:11: 31%|███ | 31/100 [00:00<00:00, 4580.22 it/sec] INFO - 20:35:11: 32%|███▏ | 32/100 [00:00<00:00, 4596.50 it/sec] INFO - 20:35:11: 33%|███▎ | 33/100 [00:00<00:00, 4610.66 it/sec] INFO - 20:35:11: 34%|███▍ | 34/100 [00:00<00:00, 4623.17 it/sec] INFO - 20:35:11: 35%|███▌ | 35/100 [00:00<00:00, 4624.66 it/sec] INFO - 20:35:11: 36%|███▌ | 36/100 [00:00<00:00, 4634.87 it/sec] INFO - 20:35:11: 37%|███▋ | 37/100 [00:00<00:00, 4633.76 it/sec] INFO - 20:35:11: 38%|███▊ | 38/100 [00:00<00:00, 4632.30 it/sec] INFO - 20:35:11: 39%|███▉ | 39/100 [00:00<00:00, 4638.66 it/sec] INFO - 20:35:11: 40%|████ | 40/100 [00:00<00:00, 4629.86 it/sec] INFO - 20:35:11: 41%|████ | 41/100 [00:00<00:00, 4632.97 it/sec] INFO - 20:35:11: 42%|████▏ | 42/100 [00:00<00:00, 4637.27 it/sec] INFO - 20:35:11: 43%|████▎ | 43/100 [00:00<00:00, 4641.15 it/sec] INFO - 20:35:11: 44%|████▍ | 44/100 [00:00<00:00, 4646.96 it/sec] INFO - 20:35:11: 45%|████▌ | 45/100 [00:00<00:00, 4640.40 it/sec] INFO - 20:35:11: 46%|████▌ | 46/100 [00:00<00:00, 4646.87 it/sec] INFO - 20:35:11: 47%|████▋ | 47/100 [00:00<00:00, 4654.73 it/sec] INFO - 20:35:11: 48%|████▊ | 48/100 [00:00<00:00, 4662.60 it/sec] INFO - 20:35:11: 49%|████▉ | 49/100 [00:00<00:00, 4669.66 it/sec] INFO - 20:35:11: 50%|█████ | 50/100 [00:00<00:00, 4666.46 it/sec] INFO - 20:35:11: 51%|█████ | 51/100 [00:00<00:00, 4672.45 it/sec] INFO - 20:35:11: 52%|█████▏ | 52/100 [00:00<00:00, 4678.53 it/sec] INFO - 20:35:11: 53%|█████▎ | 53/100 [00:00<00:00, 4684.20 it/sec] INFO - 20:35:11: 54%|█████▍ | 54/100 [00:00<00:00, 4684.43 it/sec] INFO - 20:35:11: 55%|█████▌ | 55/100 [00:00<00:00, 4684.47 it/sec] INFO - 20:35:11: 56%|█████▌ | 56/100 [00:00<00:00, 4689.27 it/sec] INFO - 20:35:11: 57%|█████▋ | 57/100 [00:00<00:00, 4694.93 it/sec] INFO - 20:35:11: 58%|█████▊ | 58/100 [00:00<00:00, 4700.50 it/sec] INFO - 20:35:11: 59%|█████▉ | 59/100 [00:00<00:00, 4695.62 it/sec] INFO - 20:35:11: 60%|██████ | 60/100 [00:00<00:00, 4697.83 it/sec] INFO - 20:35:11: 61%|██████ | 61/100 [00:00<00:00, 4649.75 it/sec] INFO - 20:35:11: 62%|██████▏ | 62/100 [00:00<00:00, 4651.42 it/sec] INFO - 20:35:11: 63%|██████▎ | 63/100 [00:00<00:00, 4648.78 it/sec] INFO - 20:35:11: 64%|██████▍ | 64/100 [00:00<00:00, 4651.78 it/sec] INFO - 20:35:11: 65%|██████▌ | 65/100 [00:00<00:00, 4654.85 it/sec] INFO - 20:35:11: 66%|██████▌ | 66/100 [00:00<00:00, 4659.55 it/sec] INFO - 20:35:11: 67%|██████▋ | 67/100 [00:00<00:00, 4664.67 it/sec] INFO - 20:35:11: 68%|██████▊ | 68/100 [00:00<00:00, 4663.92 it/sec] INFO - 20:35:11: 69%|██████▉ | 69/100 [00:00<00:00, 4660.19 it/sec] INFO - 20:35:11: 70%|███████ | 70/100 [00:00<00:00, 4659.38 it/sec] INFO - 20:35:11: 71%|███████ | 71/100 [00:00<00:00, 4660.56 it/sec] INFO - 20:35:11: 72%|███████▏ | 72/100 [00:00<00:00, 4661.42 it/sec] INFO - 20:35:11: 73%|███████▎ | 73/100 [00:00<00:00, 4657.86 it/sec] INFO - 20:35:11: 74%|███████▍ | 74/100 [00:00<00:00, 4658.03 it/sec] INFO - 20:35:11: 75%|███████▌ | 75/100 [00:00<00:00, 4660.68 it/sec] INFO - 20:35:11: 76%|███████▌ | 76/100 [00:00<00:00, 4662.79 it/sec] INFO - 20:35:11: 77%|███████▋ | 77/100 [00:00<00:00, 4661.82 it/sec] INFO - 20:35:11: 78%|███████▊ | 78/100 [00:00<00:00, 4659.41 it/sec] INFO - 20:35:11: 79%|███████▉ | 79/100 [00:00<00:00, 4661.06 it/sec] INFO - 20:35:11: 80%|████████ | 80/100 [00:00<00:00, 4663.38 it/sec] INFO - 20:35:11: 81%|████████ | 81/100 [00:00<00:00, 4666.61 it/sec] INFO - 20:35:11: 82%|████████▏ | 82/100 [00:00<00:00, 4665.02 it/sec] INFO - 20:35:11: 83%|████████▎ | 83/100 [00:00<00:00, 4666.71 it/sec] INFO - 20:35:11: 84%|████████▍ | 84/100 [00:00<00:00, 4669.91 it/sec] INFO - 20:35:11: 85%|████████▌ | 85/100 [00:00<00:00, 4672.31 it/sec] INFO - 20:35:11: 86%|████████▌ | 86/100 [00:00<00:00, 4675.02 it/sec] INFO - 20:35:11: 87%|████████▋ | 87/100 [00:00<00:00, 4672.57 it/sec] INFO - 20:35:11: 88%|████████▊ | 88/100 [00:00<00:00, 4674.92 it/sec] INFO - 20:35:11: 89%|████████▉ | 89/100 [00:00<00:00, 4678.74 it/sec] INFO - 20:35:11: 90%|█████████ | 90/100 [00:00<00:00, 4681.90 it/sec] INFO - 20:35:11: 91%|█████████ | 91/100 [00:00<00:00, 4684.99 it/sec] INFO - 20:35:11: 92%|█████████▏| 92/100 [00:00<00:00, 4683.98 it/sec] INFO - 20:35:11: 93%|█████████▎| 93/100 [00:00<00:00, 4686.54 it/sec] INFO - 20:35:11: 94%|█████████▍| 94/100 [00:00<00:00, 4690.11 it/sec] INFO - 20:35:11: 95%|█████████▌| 95/100 [00:00<00:00, 4694.32 it/sec] INFO - 20:35:11: 96%|█████████▌| 96/100 [00:00<00:00, 4698.73 it/sec] INFO - 20:35:11: 97%|█████████▋| 97/100 [00:00<00:00, 4695.08 it/sec] INFO - 20:35:11: 98%|█████████▊| 98/100 [00:00<00:00, 4697.19 it/sec] INFO - 20:35:11: 99%|█████████▉| 99/100 [00:00<00:00, 4701.44 it/sec] INFO - 20:35:11: 100%|██████████| 100/100 [00:00<00:00, 4705.35 it/sec] INFO - 20:35:11: *** End Sampling execution *** .. GENERATED FROM PYTHON SOURCE LINES 119-130 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 130-132 .. code-block:: Python model = create_regression_model("PolynomialRegressor", training_dataset, degree=3) model.learn() .. GENERATED FROM PYTHON SOURCE LINES 133-134 and see that this model seems to be better: .. GENERATED FROM PYTHON SOURCE LINES 134-141 .. 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.165 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 `_