.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/mlearning/dimension_reduction/plot_klsvd_burgers.py" .. LINE NUMBERS ARE GIVEN BELOW. .. 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_examples_mlearning_dimension_reduction_plot_klsvd_burgers.py: KL-SVD on Burgers equation ========================== Example using KL-SVD on solutions of the Burgers equation. .. GENERATED FROM PYTHON SOURCE LINES 29-40 .. code-block:: default from __future__ import division, unicode_literals import matplotlib.pyplot as plt from gemseo.api import configure_logger from gemseo.mlearning.transform.dimension_reduction.klsvd import KLSVD from gemseo.problems.dataset.burgers import BurgersDataset configure_logger() .. rst-class:: sphx-glr-script-out Out: .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 41-43 Load dataset ~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 43-50 .. code-block:: default dataset = BurgersDataset(n_samples=20) print(dataset) t = dataset.get_data_by_group(dataset.INPUT_GROUP)[:, 0] u_t = dataset.get_data_by_group(dataset.OUTPUT_GROUP) t_split = 0.87 .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Burgers Number of samples: 20 Number of variables: 2 Variables names and sizes by group: inputs: t (1) outputs: u_t (501) Number of dimensions (total = 502) by group: inputs: 1 outputs: 501 .. GENERATED FROM PYTHON SOURCE LINES 51-53 Plot dataset ~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 53-77 .. code-block:: default def lines_gen(): """Linestyle generator.""" yield "-" for i in range(1, dataset.n_samples): yield 0, (i, 1, 1, 1) color = "red" lines = lines_gen() for i in range(dataset.n_samples): # Switch mode if discontinuity is gone if color == "red" and t[i] > t_split: color = "blue" lines = lines_gen() # reset linestyle generator plt.plot(u_t[i], color=color, linestyle=next(lines), label="t={:.2f}".format(t[i])) plt.legend() plt.title("Solutions to Burgers equation") plt.show() .. image:: /examples/mlearning/dimension_reduction/images/sphx_glr_plot_klsvd_burgers_001.png :alt: Solutions to Burgers equation :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 78-80 Create KLSVD ~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 80-88 .. code-block:: default n_modes = 7 klsvd = KLSVD(dataset.metadata["x"], n_modes) klsvd.fit(u_t) u_t_reduced = klsvd.transform(u_t) u_t_restored = klsvd.inverse_transform(u_t_reduced) print("Dimension of the reduced space: {}".format(klsvd.output_dimension)) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Dimension of the reduced space: 7 .. GENERATED FROM PYTHON SOURCE LINES 89-91 Plot restored data ~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 91-109 .. code-block:: default color = "red" lines = lines_gen() for i in range(dataset.n_samples): # Switch mode if discontinuity is gone if color == "red" and t[i] > t_split: color = "blue" lines = lines_gen() # reset linestyle generator plt.plot( u_t_restored[i], color=color, # linestyle=next(lines), label="t={:.2f}".format(t[i]), ) plt.legend() plt.title("Reconstructed solution after KLSVD reduction.") plt.show() .. image:: /examples/mlearning/dimension_reduction/images/sphx_glr_plot_klsvd_burgers_002.png :alt: Reconstructed solution after KLSVD reduction. :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.524 seconds) .. _sphx_glr_download_examples_mlearning_dimension_reduction_plot_klsvd_burgers.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_klsvd_burgers.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_klsvd_burgers.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_