.. 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-28 .. code-block:: Python 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 29-34 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 34-40 .. code-block:: Python def f(x): return (6 * x - 2) ** 2 * sin(12 * x - 4) .. GENERATED FROM PYTHON SOURCE LINES 41-45 and try to approximate it with an :class:`.RBFRegressor`. For this, we can take these 7 learning input points .. GENERATED FROM PYTHON SOURCE LINES 45-47 .. 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 48-49 and evaluate the model ``f`` over this design of experiments (DOE): .. GENERATED FROM PYTHON SOURCE LINES 49-51 .. code-block:: Python y_train = f(x_train) .. GENERATED FROM PYTHON SOURCE LINES 52-54 Then, we create an :class:`.IODataset` from these 7 learning samples: .. GENERATED FROM PYTHON SOURCE LINES 54-58 .. 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 59-60 and build a :class:`.SurrogateDiscipline` from it: .. GENERATED FROM PYTHON SOURCE LINES 60-62 .. code-block:: Python surrogate_discipline = SurrogateDiscipline("RBFRegressor", dataset_train) .. GENERATED FROM PYTHON SOURCE LINES 63-65 Lastly, we can get its :class:`.R2Measure` .. GENERATED FROM PYTHON SOURCE LINES 65-67 .. code-block:: Python r2 = surrogate_discipline.get_error_measure("R2Measure") .. GENERATED FROM PYTHON SOURCE LINES 68-69 and evaluate it: .. GENERATED FROM PYTHON SOURCE LINES 69-71 .. code-block:: Python r2.compute_learning_measure() .. rst-class:: sphx-glr-script-out .. code-block:: none array([1.]) .. GENERATED FROM PYTHON SOURCE LINES 72-74 In presence of additional data, the generalization quality can also be approximated: .. GENERATED FROM PYTHON SOURCE LINES 74-82 .. 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 83-86 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.027 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 ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_