.. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_meta_modeling_general_purpose_metamodels_plot_create_linear_least_squares_model.py: Create a linear least squares model =================================== In this example we are going to create a global approximation of a model response using a linear function: .. math:: \underline{y} \, \approx \, \widehat{h}(\underline{x}) \, = \, \sum_{j=0}^{n_X} \; a_j \; \psi_j(\underline{x}) Here .. math:: h(x) = [cos(x_1 + x_2), (x2 + 1)* e^{x_1 - 2* x_2}] .. code-block:: default from __future__ import print_function import openturns as ot import openturns.viewer as viewer from matplotlib import pylab as plt ot.Log.Show(ot.Log.NONE) # Prepare an input sample x = [[0.5,0.5], [-0.5,-0.5], [-0.5,0.5], [0.5,-0.5]] x += [[0.25,0.25], [-0.25,-0.25], [-0.25,0.25], [0.25,-0.25]] Compute the output sample from the input sample and a function .. code-block:: default formulas = ['cos(x1 + x2)', '(x2 + 1) * exp(x1 - 2 * x2)'] model = ot.SymbolicFunction(['x1', 'x2'], formulas) y = model(x) create a linear least squares model .. code-block:: default algo = ot.LinearLeastSquares(x, y) algo.run() get the linear term .. code-block:: default algo.getLinear() .. raw:: html

[[ 9.93014e-17 0.998189 ]
[ 0 -0.925648 ]]



get the constant term .. code-block:: default algo.getConstant() .. raw:: html

[0.854471,1.05305]



get the metamodel .. code-block:: default responseSurface = algo.getMetaModel() plot 2nd output of our model with x1=0.5 .. code-block:: default graph = ot.ParametricFunction(responseSurface, [0], [0.5]).getMarginal(1).draw(-0.5, 0.5) graph.setLegends(['linear LS']) curve = ot.ParametricFunction(model, [0], [0.5]).getMarginal(1).draw(-0.5, 0.5).getDrawable(0) curve.setColor('red') curve.setLegend('model') graph.add(curve) graph.setLegendPosition('topright') view = viewer.View(graph) plt.show() .. image:: /auto_meta_modeling/general_purpose_metamodels/images/sphx_glr_plot_create_linear_least_squares_model_001.png :alt: y1 as a function of x1 :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.079 seconds) .. _sphx_glr_download_auto_meta_modeling_general_purpose_metamodels_plot_create_linear_least_squares_model.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_create_linear_least_squares_model.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_create_linear_least_squares_model.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_