.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/mlearning/dimension_reduction/plot_pca_burgers.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_dimension_reduction_plot_pca_burgers.py: PCA on Burgers equation ======================= Example using PCA on solutions of the Burgers equation. .. GENERATED FROM PYTHON SOURCE LINES 27-40 .. code-block:: Python from __future__ import annotations import matplotlib.pyplot as plt from numpy import eye from gemseo import configure_logger from gemseo.mlearning.transformers.dimension_reduction.pca import PCA from gemseo.problems.dataset.burgers import create_burgers_dataset configure_logger() .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 41-43 Load dataset ~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 43-50 .. code-block:: Python dataset = create_burgers_dataset(n_samples=20) dataset t = dataset.input_dataset.to_numpy()[:, 0] u_t = dataset.output_dataset.to_numpy() t_split = 0.87 .. GENERATED FROM PYTHON SOURCE LINES 51-53 Plot dataset ~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 53-76 .. code-block:: Python 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=f"t={t[i]:.2f}") plt.legend() plt.title("Solutions to Burgers equation") plt.show() .. image-sg:: /examples/mlearning/dimension_reduction/images/sphx_glr_plot_pca_burgers_001.png :alt: Solutions to Burgers equation :srcset: /examples/mlearning/dimension_reduction/images/sphx_glr_plot_pca_burgers_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 77-79 Create PCA ~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 79-89 .. code-block:: Python n_components = 7 pca = PCA(n_components=n_components) pca.fit(u_t) means = u_t.mean(axis=1) # u_t = u_t - means[:, None] u_t_reduced = pca.transform(u_t) u_t_restored = pca.inverse_transform(u_t_reduced) .. rst-class:: sphx-glr-script-out .. code-block:: none WARNING - 13:55:27: The Scaler.fit() function does nothing; the instance of Scaler uses the coefficient and offset passed at its initialization .. GENERATED FROM PYTHON SOURCE LINES 90-92 Plot restored data ~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 92-109 .. code-block:: Python 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=f"t={t[i]:.2f}", ) plt.legend() plt.title("Reconstructed solution after PCA reduction.") plt.show() .. image-sg:: /examples/mlearning/dimension_reduction/images/sphx_glr_plot_pca_burgers_002.png :alt: Reconstructed solution after PCA reduction. :srcset: /examples/mlearning/dimension_reduction/images/sphx_glr_plot_pca_burgers_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 110-112 Plot principal components ~~~~~~~~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 112-118 .. code-block:: Python red_component = eye(n_components) components = pca.inverse_transform(red_component) for i in range(n_components): plt.plot(components[i]) plt.title("Principal components") plt.show() .. image-sg:: /examples/mlearning/dimension_reduction/images/sphx_glr_plot_pca_burgers_003.png :alt: Principal components :srcset: /examples/mlearning/dimension_reduction/images/sphx_glr_plot_pca_burgers_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.679 seconds) .. _sphx_glr_download_examples_mlearning_dimension_reduction_plot_pca_burgers.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_pca_burgers.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_pca_burgers.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_