Calibration of a polynomial regression

from __future__ import absolute_import, division, print_function, unicode_literals

from builtins import round

import matplotlib.pyplot as plt
from future import standard_library
from matplotlib.tri import Triangulation

from gemseo.algos.design_space import DesignSpace
from gemseo.api import configure_logger
from gemseo.mlearning.core.calibration import MLAlgoCalibration
from gemseo.mlearning.qual_measure.mse_measure import MSEMeasure
from gemseo.problems.dataset.rosenbrock import RosenbrockDataset

standard_library.install_aliases()

Load the dataset

dataset = RosenbrockDataset(opt_naming=False, n_samples=25)

Define the measure

configure_logger()
test_dataset = RosenbrockDataset(opt_naming=False)
measure_options = {"method": "test", "test_data": test_dataset}

Calibrate the degree of the polynomial regression

Define and execute the calibration

calibration_space = DesignSpace()
calibration_space.add_variable("degree", 1, "integer", 1, 10, 1)
calibration = MLAlgoCalibration(
    "PolynomialRegression",
    dataset,
    ["degree"],
    calibration_space,
    MSEMeasure,
    measure_options,
)
calibration.execute({"algo": "fullfact", "n_samples": 10})
x_opt = calibration.optimal_parameters
f_opt = calibration.optimal_criterion
print("optimal degree:", x_opt["degree"][0])
print("optimal criterion:", f_opt)

Out:

optimal degree: 4.0
optimal criterion: 1.270764892025432e-24

Get the history

print(calibration.dataset.export_to_dataframe())

Out:

  inputs       outputs
  degree     criterion      learning
       0             0             0
0    1.0  5.888317e+05  8.200828e+05
1    2.0  1.732475e+05  2.404571e+05
2    3.0  3.001292e+04  1.645714e+04
3    4.0  1.270765e-24  2.042323e-24
4    5.0  1.097877e-01  5.385996e-24
5    6.0  1.183264e+03  6.075900e-25
6    7.0  6.895919e+03  2.103320e-23
7    8.0  1.356307e+04  2.622494e-23
8    9.0  9.180547e+04  4.803723e-23
9   10.0  1.625259e+05  4.449178e-23

Visualize the results

degree = calibration.get_history("degree")
criterion = calibration.get_history("criterion")
learning = calibration.get_history("learning")

plt.plot(degree, criterion, "-o", label="test", color="red")
plt.plot(degree, learning, "-o", label="learning", color="blue")
plt.xlabel("polynomial degree")
plt.ylabel("quality")
plt.axvline(x_opt["degree"], color="red", ls="--")
plt.legend()
plt.show()
plot calibration

Calibrate the ridge penalty of the polynomial regression

Define and execute the calibration

calibration_space = DesignSpace()
calibration_space.add_variable("penalty_level", 1, "float", 0.0, 100.0, 0.0)
calibration = MLAlgoCalibration(
    "PolynomialRegression",
    dataset,
    ["penalty_level"],
    calibration_space,
    MSEMeasure,
    measure_options,
    degree=10,
)
calibration.execute({"algo": "fullfact", "n_samples": 10})
x_opt = calibration.optimal_parameters
f_opt = calibration.optimal_criterion
print("optimal penalty_level:", x_opt["penalty_level"][0])
print("optimal criterion:", f_opt)

Out:

optimal penalty_level: 33.33333333333333
optimal criterion: 17189.5264929348

Get the history

print(calibration.dataset.export_to_dataframe())

Out:

         inputs        outputs
  penalty_level      criterion      learning
              0              0             0
0      0.000000  162525.860760  4.449178e-23
1     11.111111   32506.221289  1.087801e+03
2     22.222222   17820.599507  1.982580e+03
3     33.333333   17189.526493  2.690007e+03
4     44.444444   19953.420378  3.251453e+03
5     55.555556   23493.269988  3.703714e+03
6     66.666667   27024.053276  4.074147e+03
7     77.777778   30303.486633  4.382362e+03
8     88.888889   33272.062305  4.642448e+03
9    100.000000   35934.745536  4.864667e+03

Visualize the results

penalty_level = calibration.get_history("penalty_level")
criterion = calibration.get_history("criterion")
learning = calibration.get_history("learning")

plt.plot(penalty_level, criterion, "-o", label="test", color="red")
plt.plot(penalty_level, learning, "-o", label="learning", color="blue")
plt.axvline(x_opt["penalty_level"], color="red", ls="--")
plt.xlabel("ridge penalty")
plt.ylabel("quality")
plt.legend()
plt.show()
plot calibration

Calibrate the lasso penalty of the polynomial regression

Define and execute the calibration

calibration_space = DesignSpace()
calibration_space.add_variable("penalty_level", 1, "float", 0.0, 100.0, 0.0)
calibration = MLAlgoCalibration(
    "PolynomialRegression",
    dataset,
    ["penalty_level"],
    calibration_space,
    MSEMeasure,
    measure_options,
    degree=10,
    l2_penalty_ratio=0.0,
)
calibration.execute({"algo": "fullfact", "n_samples": 10})
x_opt = calibration.optimal_parameters
f_opt = calibration.optimal_criterion
print("optimal penalty_level:", x_opt["penalty_level"][0])
print("optimal criterion:", f_opt)

Out:

/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 53014.11952527281, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 46312.77905592318, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 22836.762840616968, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 19844.062995126646, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 10539.652780551347, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 11515.535179016297, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9925.218372422736, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 8745.639387524861, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 6371.9844124363735, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
optimal penalty_level: 11.11111111111111
optimal criterion: 15775.98958112584

Get the history

print(calibration.dataset.export_to_dataframe())

Out:

         inputs        outputs
  penalty_level      criterion      learning
              0              0             0
0      0.000000  162525.860760  4.449178e-23
1     11.111111   15775.989581  1.814382e+03
2     22.222222   31529.584354  4.057302e+03
3     33.333333   47420.249503  5.792299e+03
4     44.444444   59358.207437  7.169565e+03
5     55.555556   62656.171431  7.278397e+03
6     66.666667   66256.259889  7.410137e+03
7     77.777778   69336.190346  7.540731e+03
8     88.888889   72457.378777  7.675963e+03
9    100.000000   75749.793494  7.816545e+03

Visualize the results

penalty_level = calibration.get_history("penalty_level")
criterion = calibration.get_history("criterion")
learning = calibration.get_history("learning")

plt.plot(penalty_level, criterion, "-o", label="test", color="red")
plt.plot(penalty_level, learning, "-o", label="learning", color="blue")
plt.axvline(x_opt["penalty_level"], color="red", ls="--")
plt.xlabel("lasso penalty")
plt.ylabel("quality")
plt.legend()
plt.show()
plot calibration

Calibrate the elasticnet penalty of the polynomial regression

Define and execute the calibration

calibration_space = DesignSpace()
calibration_space.add_variable("penalty_level", 1, "float", 0.0, 40.0, 0.0)
calibration_space.add_variable("l2_penalty_ratio", 1, "float", 0.0, 1.0, 0.5)
calibration = MLAlgoCalibration(
    "PolynomialRegression",
    dataset,
    ["penalty_level", "l2_penalty_ratio"],
    calibration_space,
    MSEMeasure,
    measure_options,
    degree=10,
)
calibration.execute({"algo": "fullfact", "n_samples": 100})
x_opt = calibration.optimal_parameters
f_opt = calibration.optimal_criterion
print("optimal penalty_level:", x_opt["penalty_level"][0])
print("optimal l2_penalty_ratio:", x_opt["l2_penalty_ratio"][0])
print("optimal criterion:", f_opt)

Out:

/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 29043.941873262476, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 17861.242207899086, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 23248.369020079124, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 40337.592310345935, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 46312.77905592318, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 41663.11234403636, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 19678.93630810287, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 25448.221246517962, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 28112.625869984273, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 52327.379969754176, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 60323.82550357153, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 49912.85388970988, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 53260.502122664984, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 51004.324032207056, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 46356.76266880178, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 40105.01306439645, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 43759.23050117445, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 32737.6383574511, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 54160.34846162713, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 70494.26467152775, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 71136.63093822205, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 68792.4637923982, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 63459.39289327263, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 51385.13766746219, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 30876.534229431825, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 26141.488265132284, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 24190.211027816127, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 64450.33236753119, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 74891.63916846788, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 77196.15850180795, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 73552.79972675092, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 59011.058667527715, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 49116.956434006184, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 48133.69228252996, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 45754.86056797807, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 42282.73810280081, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 70242.51600023739, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 80113.69706210528, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 81128.3587537658, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 75191.01350819836, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 60549.874831144545, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 61441.387341868096, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 62785.52398195918, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 61079.903585404085, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 72380.74554461434, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 74313.77388861046, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 83635.41756075562, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 84127.30452105973, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 79426.0212278234, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 73365.24131195752, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 73686.88102281588, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 76500.00157023198, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 78920.80461385961, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 86505.78991687924, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 77400.21108157154, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 86487.02367866725, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 88435.62342717669, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 79838.89049765863, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 81308.80235178744, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 86023.17726375742, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 87320.20191975817, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 91344.83957996666, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 84962.34972659523, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 79998.05160984234, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 88966.87593987946, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 91475.65007848587, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 87062.52528431122, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 91948.95998352996, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 94655.4499929364, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 95795.91888194041, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 92577.41520459115, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 80204.40914621703, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 82215.63093180019, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 91794.83776150897, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 94456.9098907501, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 95065.41953052203, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 99355.28336318409, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 100496.24127551212, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 100955.79720587714, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 98772.7683944503, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 98656.18825020781, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
optimal penalty_level: 4.444444444444445
optimal l2_penalty_ratio: 0.0
optimal criterion: 4136.820826715572

Get the history

print(calibration.dataset.export_to_dataframe())

Out:

          inputs                         outputs
   penalty_level l2_penalty_ratio      criterion      learning
               0                0              0             0
0       0.000000              0.0  162525.860760  4.449178e-23
1       4.444444              0.0    4136.820827  4.546714e+02
2       8.888889              0.0   13371.034446  1.375915e+03
3      13.333333              0.0   17860.819693  2.176736e+03
4      17.777778              0.0   23914.366014  3.005032e+03
..           ...              ...            ...           ...
95     22.222222              1.0   17820.599507  1.982580e+03
96     26.666667              1.0   16816.595592  2.285780e+03
97     31.111111              1.0   16894.821607  2.561538e+03
98     35.555556              1.0   17602.178769  2.812662e+03
99     40.000000              1.0   18674.751406  3.041823e+03

[100 rows x 4 columns]

Visualize the results

penalty_level = calibration.get_history("penalty_level").flatten()
l2_penalty_ratio = calibration.get_history("l2_penalty_ratio").flatten()
criterion = calibration.get_history("criterion").flatten()
learning = calibration.get_history("learning").flatten()

triang = Triangulation(penalty_level, l2_penalty_ratio)

fig = plt.figure()
ax = fig.add_subplot(1, 2, 1)
ax.tricontourf(triang, criterion, cmap="Purples")
ax.scatter(x_opt["penalty_level"][0], x_opt["l2_penalty_ratio"][0])
ax.set_xlabel("penalty level")
ax.set_ylabel("l2 penalty ratio")
ax.set_title("Test measure")
ax = fig.add_subplot(1, 2, 2)
ax.tricontourf(triang, learning, cmap="Purples")
ax.scatter(x_opt["penalty_level"][0], x_opt["l2_penalty_ratio"][0])
ax.set_xlabel("penalty level")
ax.set_ylabel("l2 penalty ratio")
ax.set_title("Learning measure")

plt.show()
Test measure, Learning measure

Add an optimization stage

calibration_space = DesignSpace()
calibration_space.add_variable("penalty_level", 1, "float", 0.0, 40.0, 0.0)
calibration_space.add_variable("l2_penalty_ratio", 1, "float", 0.0, 1.0, 0.5)
calibration = MLAlgoCalibration(
    "PolynomialRegression",
    dataset,
    ["penalty_level", "l2_penalty_ratio"],
    calibration_space,
    MSEMeasure,
    measure_options,
    degree=10,
    use_doe=False,
)
calibration.execute({"algo": "NLOPT_COBYLA", "max_iter": 100})
x_opt2 = calibration.optimal_parameters
f_opt2 = calibration.optimal_criterion

fig = plt.figure()
ax = fig.add_subplot(1, 2, 1)
ax.tricontourf(triang, criterion, cmap="Purples")
ax.scatter(x_opt["penalty_level"][0], x_opt["l2_penalty_ratio"][0])
ax.scatter(x_opt2["penalty_level"][0], x_opt2["l2_penalty_ratio"][0], color="red")
ax.set_xlabel("penalty level")
ax.set_ylabel("l2 penalty ratio")
ax.set_title("Test measure")
ax = fig.add_subplot(1, 2, 2)
ax.tricontourf(triang, learning, cmap="Purples")
ax.scatter(x_opt["penalty_level"][0], x_opt["l2_penalty_ratio"][0])
ax.scatter(x_opt2["penalty_level"][0], x_opt2["l2_penalty_ratio"][0], color="red")
ax.set_xlabel("penalty level")
ax.set_ylabel("l2 penalty ratio")
ax.set_title("Learning measure")
plt.show()

n_iterations = len(calibration.scenario.disciplines[0].cache)
print("MSE with DOE: {} (100 evaluations)".format(f_opt))
print("MSE with OPT: {} ({} evaluations)".format(f_opt2, n_iterations))
print("MSE reduction:{}%".format(round((f_opt2 - f_opt) / f_opt * 100)))
Test measure, Learning measure

Out:

/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 82866.87944006122, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 89524.2977591, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 71179.70573387566, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 80888.49322377346, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 79727.48832857361, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 74015.51423530807, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 60591.135974715275, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 14284.518255657937, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 31213.890569437783, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 11514.853703010187, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/conda/3.0.3/lib/python3.8/site-packages/sklearn/linear_model/_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 3718.3940922714746, tolerance: 2850.2270000000003
  model = cd_fast.enet_coordinate_descent(
MSE with DOE: 4136.820826715572 (100 evaluations)
MSE with OPT: 478.14128242553414 (64 evaluations)
MSE reduction:-88%

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

Gallery generated by Sphinx-Gallery