.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/mlearning/quality_measure/plot_from_surrogate.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_quality_measure_plot_from_surrogate.py: Error from surrogate discipline =============================== .. GENERATED FROM PYTHON SOURCE LINES 20-30 .. code-block:: Python from __future__ import annotations from numpy import array from numpy import newaxis from numpy import sin from gemseo.datasets.io_dataset import IODataset from gemseo.disciplines.surrogate import SurrogateDiscipline .. GENERATED FROM PYTHON SOURCE LINES 31-36 The quality of a :class:`.SurrogateDiscipline` can easily be quantified from its methods :meth:`~.SurrogateDiscipline.get_error_measure`. To illustrate this point, let us consider the function :math:`f(x)=(6x-2)^2\sin(12x-4)` :cite:`forrester2008`: .. GENERATED FROM PYTHON SOURCE LINES 36-42 .. code-block:: Python def f(x): return (6 * x - 2) ** 2 * sin(12 * x - 4) .. GENERATED FROM PYTHON SOURCE LINES 43-47 and try to approximate it with an :class:`.RBFRegressor`. For this, we can take these 7 learning input points .. GENERATED FROM PYTHON SOURCE LINES 47-49 .. code-block:: Python x_train = array([0.1, 0.3, 0.5, 0.6, 0.8, 0.9, 0.95]) .. GENERATED FROM PYTHON SOURCE LINES 50-51 and evaluate the model ``f`` over this design of experiments (DOE): .. GENERATED FROM PYTHON SOURCE LINES 51-53 .. code-block:: Python y_train = f(x_train) .. GENERATED FROM PYTHON SOURCE LINES 54-56 Then, we create an :class:`.IODataset` from these 7 learning samples: .. GENERATED FROM PYTHON SOURCE LINES 56-60 .. code-block:: Python dataset_train = IODataset() dataset_train.add_input_group(x_train[:, newaxis], ["x"]) dataset_train.add_output_group(y_train[:, newaxis], ["y"]) .. GENERATED FROM PYTHON SOURCE LINES 61-62 and build a :class:`.SurrogateDiscipline` from it: .. GENERATED FROM PYTHON SOURCE LINES 62-64 .. code-block:: Python surrogate_discipline = SurrogateDiscipline("RBFRegressor", dataset_train) .. GENERATED FROM PYTHON SOURCE LINES 65-67 Lastly, we can get its :class:`.R2Measure` .. GENERATED FROM PYTHON SOURCE LINES 67-69 .. code-block:: Python r2 = surrogate_discipline.get_error_measure("R2Measure") .. GENERATED FROM PYTHON SOURCE LINES 70-71 and evaluate it: .. GENERATED FROM PYTHON SOURCE LINES 71-73 .. code-block:: Python r2.compute_learning_measure() .. rst-class:: sphx-glr-script-out .. code-block:: none array([1.]) .. GENERATED FROM PYTHON SOURCE LINES 74-76 In presence of additional data, the generalization quality can also be approximated: .. GENERATED FROM PYTHON SOURCE LINES 76-84 .. code-block:: Python x_test = array([0.2, 0.4, 0.7]) y_test = f(x_test) dataset_test = IODataset() dataset_test.add_input_group(x_test[:, newaxis], ["x"]) dataset_test.add_output_group(y_test[:, newaxis], ["y"]) result = r2.compute_test_measure(dataset_test) r2.compute_test_measure(dataset_test) .. rst-class:: sphx-glr-script-out .. code-block:: none array([0.98752693]) .. GENERATED FROM PYTHON SOURCE LINES 85-88 We can conclude that the regression model on which the :class:`.SurrogateDiscipline` is based is a very good approximation of the original function :math:`f`. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.033 seconds) .. _sphx_glr_download_examples_mlearning_quality_measure_plot_from_surrogate.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_from_surrogate.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_from_surrogate.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_from_surrogate.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_