.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/surrogates/gpr/plot_gpr_sine.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 or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_surrogates_gpr_plot_gpr_sine.py: Gaussian Process of a sinusoidal function ====================================================================== .. GENERATED FROM PYTHON SOURCE LINES 9-12 In this example, Gaussian Process Regression is used to generate a surrogate model for a given data. In this data, sample points are generated using TrueStratifiedSampling class and functional value at sample points are estimated using a model defined in python script ('python_model_function.py). .. GENERATED FROM PYTHON SOURCE LINES 17-19 Import the necessary libraries. Here we import standard libraries such as numpy and matplotlib, but also need to import the TrueStratifiedSampling, RunModel and GaussianProcessRegression class from UQpy. .. GENERATED FROM PYTHON SOURCE LINES 22-34 .. code-block:: default import shutil from UQpy import PythonModel from UQpy.sampling.stratified_sampling.strata import RectangularStrata from UQpy.sampling import TrueStratifiedSampling from UQpy.run_model.RunModel import RunModel from UQpy.distributions import Gamma import numpy as np import matplotlib.pyplot as plt from UQpy.surrogates import GaussianProcessRegression .. GENERATED FROM PYTHON SOURCE LINES 35-36 Create a distribution object. .. GENERATED FROM PYTHON SOURCE LINES 39-43 .. code-block:: default from UQpy.utilities import RBF marginals = [Gamma(a=2., loc=1., scale=3.)] .. GENERATED FROM PYTHON SOURCE LINES 44-45 Create a distribution object. .. GENERATED FROM PYTHON SOURCE LINES 48-51 .. code-block:: default strata = RectangularStrata(strata_number=[20]) .. GENERATED FROM PYTHON SOURCE LINES 52-53 Run stratified sampling .. GENERATED FROM PYTHON SOURCE LINES 56-61 .. code-block:: default x = TrueStratifiedSampling(distributions=marginals, strata_object=strata, nsamples_per_stratum=1, random_state=2) .. GENERATED FROM PYTHON SOURCE LINES 62-64 RunModel is used to evaluate function values at sample points. Model is defined as a function in python file 'python_model_function.py'. .. GENERATED FROM PYTHON SOURCE LINES 67-85 .. code-block:: default model = PythonModel(model_script='local_python_model_1Dfunction.py', model_object_name='y_func', delete_files=True) rmodel = RunModel(model=model) rmodel.run(samples=x.samples) from UQpy.surrogates.gaussian_process.regression_models import LinearRegression from UQpy.utilities.MinimizeOptimizer import MinimizeOptimizer bounds = [[10**(-3), 10**3], [10**(-3), 10**2]] optimizer = MinimizeOptimizer(method='L-BFGS-B', bounds=bounds) K = GaussianProcessRegression(regression_model=LinearRegression(), kernel=RBF(), optimizer=optimizer, optimizations_number=20, hyperparameters=[1, 0.1], random_state=2) K.fit(samples=x.samples, values=rmodel.qoi_list) print(K.hyperparameters) .. rst-class:: sphx-glr-script-out .. code-block:: none [2.62770035 1.5096804 ] .. GENERATED FROM PYTHON SOURCE LINES 86-88 RunModel is used to evaluate function values at sample points. Model is defined as a function in python file 'python_model_function.py'. .. GENERATED FROM PYTHON SOURCE LINES 91-98 .. code-block:: default num = 1000 x1 = np.linspace(min(x.samples), max(x.samples), num) y, y_sd = K.predict(x1.reshape([num, 1]), return_std=True) .. GENERATED FROM PYTHON SOURCE LINES 99-100 Actual model is evaluated at all points to compare it with kriging surrogate. .. GENERATED FROM PYTHON SOURCE LINES 103-106 .. code-block:: default rmodel.run(samples=x1, append_samples=False) .. GENERATED FROM PYTHON SOURCE LINES 107-109 This plot shows the input data as blue dot, blue curve is actual function and orange curve represents response curve. This plot also shows the gradient and 95% confidence interval of the kriging surrogate. .. GENERATED FROM PYTHON SOURCE LINES 112-129 .. code-block:: default fig = plt.figure() ax = plt.subplot(111) plt.plot(np.squeeze(x1), np.squeeze(rmodel.qoi_list), label='Sine') plt.plot(x1, y, label='Surrogate') # plt.plot(x1, y_grad, label='Gradient') plt.scatter(K.samples, K.values, label='Data') plt.fill(np.concatenate([x1, x1[::-1]]), np.concatenate([y - 1.9600 * y_sd, (y + 1.9600 * y_sd)[::-1]]), alpha=.5, fc='y', ec='None', label='95% CI') box = ax.get_position() ax.set_position([box.x0, box.y0, box.width * 0.8, box.height]) # Put a legend to the right of the current axis ax.legend(loc='center left', bbox_to_anchor=(1, 0.5)) plt.show() .. image-sg:: /auto_examples/surrogates/gpr/images/sphx_glr_plot_gpr_sine_001.png :alt: plot gpr sine :srcset: /auto_examples/surrogates/gpr/images/sphx_glr_plot_gpr_sine_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.546 seconds) .. _sphx_glr_download_auto_examples_surrogates_gpr_plot_gpr_sine.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/SURGroup/UQpy/master?urlpath=lab/tree/notebooks/auto_examples/surrogates/gpr/plot_gpr_sine.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_gpr_sine.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_gpr_sine.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_