PCA on Burgers equation

Example using PCA on solutions of the Burgers equation.

from __future__ import division, unicode_literals

import matplotlib.pyplot as plt
from numpy import eye

from gemseo.api import configure_logger
from gemseo.mlearning.transform.dimension_reduction.pca import PCA
from gemseo.problems.dataset.burgers import BurgersDataset

configure_logger()

Out:

<RootLogger root (INFO)>

Load dataset

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

Out:

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

Plot dataset

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()
Solutions to Burgers equation

Create PCA

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)

Plot restored data

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 PCA reduction.")
plt.show()
Reconstructed solution after PCA reduction.

Plot principal components

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()
Principal components

Total running time of the script: ( 0 minutes 0.584 seconds)

Gallery generated by Sphinx-Gallery