Note
Go to the end to download the full example code.
Polynomial chaos expansion (PCE)#
A PCERegressor
is a PCE model
based on OpenTURNS.
from __future__ import annotations
from matplotlib import pyplot as plt
from numpy import array
from gemseo import configure_logger
from gemseo import create_discipline
from gemseo import create_parameter_space
from gemseo import sample_disciplines
from gemseo.mlearning import create_regression_model
configure_logger()
<RootLogger root (INFO)>
Problem#
In this example,
we represent the function \(f(x)=(6x-2)^2\sin(12x-4)\) [FSK08]
by the AnalyticDiscipline
discipline = create_discipline(
"AnalyticDiscipline",
name="f",
expressions={"y": "(6*x-2)**2*sin(12*x-4)"},
)
and seek to approximate it over the input space
input_space = create_parameter_space()
input_space.add_random_variable("x", "OTUniformDistribution")
To do this, we create a training dataset with 6 equispaced points:
training_dataset = sample_disciplines(
[discipline], input_space, "y", algo_name="PYDOE_FULLFACT", n_samples=10
)
WARNING - 00:09:41: No coupling in MDA, switching chain_linearize to True.
INFO - 00:09:41: *** Start Sampling execution ***
INFO - 00:09:41: Sampling
INFO - 00:09:41: Disciplines: f
INFO - 00:09:41: MDO formulation: MDF
INFO - 00:09:41: Running the algorithm PYDOE_FULLFACT:
INFO - 00:09:41: 10%|█ | 1/10 [00:00<00:00, 669.91 it/sec]
INFO - 00:09:41: 20%|██ | 2/10 [00:00<00:00, 1079.34 it/sec]
INFO - 00:09:41: 30%|███ | 3/10 [00:00<00:00, 1408.27 it/sec]
INFO - 00:09:41: 40%|████ | 4/10 [00:00<00:00, 1677.89 it/sec]
INFO - 00:09:41: 50%|█████ | 5/10 [00:00<00:00, 1901.83 it/sec]
INFO - 00:09:41: 60%|██████ | 6/10 [00:00<00:00, 2076.73 it/sec]
INFO - 00:09:41: 70%|███████ | 7/10 [00:00<00:00, 2232.71 it/sec]
INFO - 00:09:41: 80%|████████ | 8/10 [00:00<00:00, 2375.54 it/sec]
INFO - 00:09:41: 90%|█████████ | 9/10 [00:00<00:00, 2504.23 it/sec]
INFO - 00:09:41: 100%|██████████| 10/10 [00:00<00:00, 2605.16 it/sec]
INFO - 00:09:41: *** End Sampling execution (time: 0:00:00.005014) ***
Basics#
Training#
Then, we train an PCE regression model from these samples:
model = create_regression_model("PCERegressor", training_dataset)
model.learn()
WARNING - 00:09:41: Remove input data transformation because PCERegressor does not support transformers.
Prediction#
Once it is built, we can predict the output value of \(f\) at a new input point:
input_value = {"x": array([0.65])}
output_value = model.predict(input_value)
output_value
{'y': array([-0.81106394])}
as well as its Jacobian value:
jacobian_value = model.predict_jacobian(input_value)
jacobian_value
{'y': {'x': array([[18.2279622]])}}
Plotting#
Of course, you can see that the quadratic model is no good at all here:
test_dataset = sample_disciplines(
[discipline], input_space, "y", algo_name="PYDOE_FULLFACT", n_samples=100
)
input_data = test_dataset.get_view(variable_names=model.input_names).to_numpy()
reference_output_data = test_dataset.get_view(variable_names="y").to_numpy().ravel()
predicted_output_data = model.predict(input_data).ravel()
plt.plot(input_data.ravel(), reference_output_data, label="Reference")
plt.plot(input_data.ravel(), predicted_output_data, label="Regression - Basics")
plt.grid()
plt.legend()
plt.show()

WARNING - 00:09:41: No coupling in MDA, switching chain_linearize to True.
INFO - 00:09:41: *** Start Sampling execution ***
INFO - 00:09:41: Sampling
INFO - 00:09:41: Disciplines: f
INFO - 00:09:41: MDO formulation: MDF
INFO - 00:09:41: Running the algorithm PYDOE_FULLFACT:
INFO - 00:09:41: 1%| | 1/100 [00:00<00:00, 3457.79 it/sec]
INFO - 00:09:41: 2%|▏ | 2/100 [00:00<00:00, 3298.71 it/sec]
INFO - 00:09:41: 3%|▎ | 3/100 [00:00<00:00, 3456.84 it/sec]
INFO - 00:09:41: 4%|▍ | 4/100 [00:00<00:00, 3590.25 it/sec]
INFO - 00:09:41: 5%|▌ | 5/100 [00:00<00:00, 3687.62 it/sec]
INFO - 00:09:41: 6%|▌ | 6/100 [00:00<00:00, 3705.22 it/sec]
INFO - 00:09:41: 7%|▋ | 7/100 [00:00<00:00, 3770.40 it/sec]
INFO - 00:09:41: 8%|▊ | 8/100 [00:00<00:00, 3824.74 it/sec]
INFO - 00:09:41: 9%|▉ | 9/100 [00:00<00:00, 3871.27 it/sec]
INFO - 00:09:41: 10%|█ | 10/100 [00:00<00:00, 3871.43 it/sec]
INFO - 00:09:41: 11%|█ | 11/100 [00:00<00:00, 3903.99 it/sec]
INFO - 00:09:41: 12%|█▏ | 12/100 [00:00<00:00, 3926.03 it/sec]
INFO - 00:09:41: 13%|█▎ | 13/100 [00:00<00:00, 3954.02 it/sec]
INFO - 00:09:41: 14%|█▍ | 14/100 [00:00<00:00, 3956.89 it/sec]
INFO - 00:09:41: 15%|█▌ | 15/100 [00:00<00:00, 3979.42 it/sec]
INFO - 00:09:41: 16%|█▌ | 16/100 [00:00<00:00, 4002.44 it/sec]
INFO - 00:09:41: 17%|█▋ | 17/100 [00:00<00:00, 4025.47 it/sec]
INFO - 00:09:41: 18%|█▊ | 18/100 [00:00<00:00, 4025.89 it/sec]
INFO - 00:09:41: 19%|█▉ | 19/100 [00:00<00:00, 4032.17 it/sec]
INFO - 00:09:41: 20%|██ | 20/100 [00:00<00:00, 4046.41 it/sec]
INFO - 00:09:41: 21%|██ | 21/100 [00:00<00:00, 4060.50 it/sec]
INFO - 00:09:41: 22%|██▏ | 22/100 [00:00<00:00, 4075.92 it/sec]
INFO - 00:09:41: 23%|██▎ | 23/100 [00:00<00:00, 4072.31 it/sec]
INFO - 00:09:41: 24%|██▍ | 24/100 [00:00<00:00, 4086.85 it/sec]
INFO - 00:09:41: 25%|██▌ | 25/100 [00:00<00:00, 4098.56 it/sec]
INFO - 00:09:41: 26%|██▌ | 26/100 [00:00<00:00, 4109.58 it/sec]
INFO - 00:09:41: 27%|██▋ | 27/100 [00:00<00:00, 4101.49 it/sec]
INFO - 00:09:41: 28%|██▊ | 28/100 [00:00<00:00, 4107.89 it/sec]
INFO - 00:09:41: 29%|██▉ | 29/100 [00:00<00:00, 4112.06 it/sec]
INFO - 00:09:41: 30%|███ | 30/100 [00:00<00:00, 4120.95 it/sec]
INFO - 00:09:41: 31%|███ | 31/100 [00:00<00:00, 4104.02 it/sec]
INFO - 00:09:41: 32%|███▏ | 32/100 [00:00<00:00, 4110.30 it/sec]
INFO - 00:09:41: 33%|███▎ | 33/100 [00:00<00:00, 4120.02 it/sec]
INFO - 00:09:41: 34%|███▍ | 34/100 [00:00<00:00, 4129.21 it/sec]
INFO - 00:09:41: 35%|███▌ | 35/100 [00:00<00:00, 4125.93 it/sec]
INFO - 00:09:41: 36%|███▌ | 36/100 [00:00<00:00, 4131.75 it/sec]
INFO - 00:09:41: 37%|███▋ | 37/100 [00:00<00:00, 4140.04 it/sec]
INFO - 00:09:41: 38%|███▊ | 38/100 [00:00<00:00, 4148.99 it/sec]
INFO - 00:09:41: 39%|███▉ | 39/100 [00:00<00:00, 4158.16 it/sec]
INFO - 00:09:41: 40%|████ | 40/100 [00:00<00:00, 4154.42 it/sec]
INFO - 00:09:41: 41%|████ | 41/100 [00:00<00:00, 4161.72 it/sec]
INFO - 00:09:41: 42%|████▏ | 42/100 [00:00<00:00, 4169.09 it/sec]
INFO - 00:09:41: 43%|████▎ | 43/100 [00:00<00:00, 4175.08 it/sec]
INFO - 00:09:41: 44%|████▍ | 44/100 [00:00<00:00, 4172.40 it/sec]
INFO - 00:09:41: 45%|████▌ | 45/100 [00:00<00:00, 4176.30 it/sec]
INFO - 00:09:41: 46%|████▌ | 46/100 [00:00<00:00, 4178.86 it/sec]
INFO - 00:09:41: 47%|████▋ | 47/100 [00:00<00:00, 4183.27 it/sec]
INFO - 00:09:41: 48%|████▊ | 48/100 [00:00<00:00, 4182.11 it/sec]
INFO - 00:09:41: 49%|████▉ | 49/100 [00:00<00:00, 4184.14 it/sec]
INFO - 00:09:41: 50%|█████ | 50/100 [00:00<00:00, 4190.03 it/sec]
INFO - 00:09:41: 51%|█████ | 51/100 [00:00<00:00, 4195.46 it/sec]
INFO - 00:09:41: 52%|█████▏ | 52/100 [00:00<00:00, 4200.28 it/sec]
INFO - 00:09:41: 53%|█████▎ | 53/100 [00:00<00:00, 4195.97 it/sec]
INFO - 00:09:41: 54%|█████▍ | 54/100 [00:00<00:00, 4200.06 it/sec]
INFO - 00:09:41: 55%|█████▌ | 55/100 [00:00<00:00, 4204.78 it/sec]
INFO - 00:09:41: 56%|█████▌ | 56/100 [00:00<00:00, 4210.24 it/sec]
INFO - 00:09:41: 57%|█████▋ | 57/100 [00:00<00:00, 4208.33 it/sec]
INFO - 00:09:41: 58%|█████▊ | 58/100 [00:00<00:00, 4211.80 it/sec]
INFO - 00:09:41: 59%|█████▉ | 59/100 [00:00<00:00, 4215.02 it/sec]
INFO - 00:09:41: 60%|██████ | 60/100 [00:00<00:00, 4219.27 it/sec]
INFO - 00:09:41: 61%|██████ | 61/100 [00:00<00:00, 4217.26 it/sec]
INFO - 00:09:41: 62%|██████▏ | 62/100 [00:00<00:00, 4190.86 it/sec]
INFO - 00:09:41: 63%|██████▎ | 63/100 [00:00<00:00, 4189.85 it/sec]
INFO - 00:09:41: 64%|██████▍ | 64/100 [00:00<00:00, 4192.27 it/sec]
INFO - 00:09:41: 65%|██████▌ | 65/100 [00:00<00:00, 4190.05 it/sec]
INFO - 00:09:41: 66%|██████▌ | 66/100 [00:00<00:00, 4192.65 it/sec]
INFO - 00:09:41: 67%|██████▋ | 67/100 [00:00<00:00, 4195.43 it/sec]
INFO - 00:09:41: 68%|██████▊ | 68/100 [00:00<00:00, 4199.37 it/sec]
INFO - 00:09:41: 69%|██████▉ | 69/100 [00:00<00:00, 4199.23 it/sec]
INFO - 00:09:41: 70%|███████ | 70/100 [00:00<00:00, 4200.85 it/sec]
INFO - 00:09:41: 71%|███████ | 71/100 [00:00<00:00, 4204.25 it/sec]
INFO - 00:09:41: 72%|███████▏ | 72/100 [00:00<00:00, 4207.92 it/sec]
INFO - 00:09:41: 73%|███████▎ | 73/100 [00:00<00:00, 4210.40 it/sec]
INFO - 00:09:41: 74%|███████▍ | 74/100 [00:00<00:00, 4207.04 it/sec]
INFO - 00:09:41: 75%|███████▌ | 75/100 [00:00<00:00, 4209.46 it/sec]
INFO - 00:09:41: 76%|███████▌ | 76/100 [00:00<00:00, 4212.48 it/sec]
INFO - 00:09:41: 77%|███████▋ | 77/100 [00:00<00:00, 4214.94 it/sec]
INFO - 00:09:41: 78%|███████▊ | 78/100 [00:00<00:00, 4213.43 it/sec]
INFO - 00:09:41: 79%|███████▉ | 79/100 [00:00<00:00, 4213.13 it/sec]
INFO - 00:09:41: 80%|████████ | 80/100 [00:00<00:00, 4213.69 it/sec]
INFO - 00:09:41: 81%|████████ | 81/100 [00:00<00:00, 4214.75 it/sec]
INFO - 00:09:41: 82%|████████▏ | 82/100 [00:00<00:00, 4212.95 it/sec]
INFO - 00:09:41: 83%|████████▎ | 83/100 [00:00<00:00, 4214.87 it/sec]
INFO - 00:09:41: 84%|████████▍ | 84/100 [00:00<00:00, 4217.35 it/sec]
INFO - 00:09:41: 85%|████████▌ | 85/100 [00:00<00:00, 4219.77 it/sec]
INFO - 00:09:41: 86%|████████▌ | 86/100 [00:00<00:00, 4221.55 it/sec]
INFO - 00:09:41: 87%|████████▋ | 87/100 [00:00<00:00, 4218.40 it/sec]
INFO - 00:09:41: 88%|████████▊ | 88/100 [00:00<00:00, 4220.01 it/sec]
INFO - 00:09:41: 89%|████████▉ | 89/100 [00:00<00:00, 4222.29 it/sec]
INFO - 00:09:41: 90%|█████████ | 90/100 [00:00<00:00, 4225.05 it/sec]
INFO - 00:09:41: 91%|█████████ | 91/100 [00:00<00:00, 4224.15 it/sec]
INFO - 00:09:41: 92%|█████████▏| 92/100 [00:00<00:00, 4226.00 it/sec]
INFO - 00:09:41: 93%|█████████▎| 93/100 [00:00<00:00, 4227.90 it/sec]
INFO - 00:09:41: 94%|█████████▍| 94/100 [00:00<00:00, 4229.17 it/sec]
INFO - 00:09:41: 95%|█████████▌| 95/100 [00:00<00:00, 4227.77 it/sec]
INFO - 00:09:41: 96%|█████████▌| 96/100 [00:00<00:00, 4228.93 it/sec]
INFO - 00:09:41: 97%|█████████▋| 97/100 [00:00<00:00, 4231.38 it/sec]
INFO - 00:09:41: 98%|█████████▊| 98/100 [00:00<00:00, 4232.00 it/sec]
INFO - 00:09:41: 99%|█████████▉| 99/100 [00:00<00:00, 4234.90 it/sec]
INFO - 00:09:41: 100%|██████████| 100/100 [00:00<00:00, 4233.16 it/sec]
INFO - 00:09:41: *** End Sampling execution (time: 0:00:00.025480) ***
Settings#
The PCERegressor
has many options
defined in the PCERegressor_Settings
Pydantic model.
Degree#
model = create_regression_model("PCERegressor", training_dataset, degree=3)
model.learn()
WARNING - 00:09:41: Remove input data transformation because PCERegressor does not support transformers.
and see that this model seems to be better:
predicted_output_data_ = model.predict(input_data).ravel()
plt.plot(input_data.ravel(), reference_output_data, label="Reference")
plt.plot(input_data.ravel(), predicted_output_data, label="Regression - Basics")
plt.plot(input_data.ravel(), predicted_output_data_, label="Regression - Degree(3)")
plt.grid()
plt.legend()
plt.show()

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