Random forest#

A RandomForestRegressor is a random forest model based on scikit-learn.

from __future__ import annotations

from matplotlib import pyplot as plt
from numpy import array

from gemseo import configure_logger
from gemseo import create_design_space
from gemseo import create_discipline
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_design_space()
input_space.add_variable("x", lower_bound=0.0, upper_bound=1.0)

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=6
)
WARNING - 20:35:13: No coupling in MDA, switching chain_linearize to True.
   INFO - 20:35:13: *** Start Sampling execution ***
   INFO - 20:35:13: Sampling
   INFO - 20:35:13:    Disciplines: f
   INFO - 20:35:13:    MDO formulation: MDF
   INFO - 20:35:13: Running the algorithm PYDOE_FULLFACT:
   INFO - 20:35:13:     17%|█▋        | 1/6 [00:00<00:00, 638.01 it/sec]
   INFO - 20:35:13:     33%|███▎      | 2/6 [00:00<00:00, 1049.63 it/sec]
   INFO - 20:35:13:     50%|█████     | 3/6 [00:00<00:00, 1373.38 it/sec]
   INFO - 20:35:13:     67%|██████▋   | 4/6 [00:00<00:00, 1657.83 it/sec]
   INFO - 20:35:13:     83%|████████▎ | 5/6 [00:00<00:00, 1902.52 it/sec]
   INFO - 20:35:13:    100%|██████████| 6/6 [00:00<00:00, 2113.53 it/sec]
   INFO - 20:35:13: *** End Sampling execution ***

Basics#

Training#

Then, we train an random forest regression model from these samples:

model = create_regression_model("RandomForestRegressor", training_dataset)
model.learn()

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.88837697])}

but cannot predict its Jacobian value:

try:
    model.predict_jacobian(input_value)
except NotImplementedError:
    print("The derivatives are not available for RandomForestRegressor.")
The derivatives are not available for RandomForestRegressor.

Plotting#

You can see that the random forest model is pretty good on the left, but bad on the right:

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()
plot random forest regression
WARNING - 20:35:13: No coupling in MDA, switching chain_linearize to True.
   INFO - 20:35:13: *** Start Sampling execution ***
   INFO - 20:35:13: Sampling
   INFO - 20:35:13:    Disciplines: f
   INFO - 20:35:13:    MDO formulation: MDF
   INFO - 20:35:13: Running the algorithm PYDOE_FULLFACT:
   INFO - 20:35:13:      1%|          | 1/100 [00:00<00:00, 2922.86 it/sec]
   INFO - 20:35:13:      2%|▏         | 2/100 [00:00<00:00, 2861.05 it/sec]
   INFO - 20:35:13:      3%|▎         | 3/100 [00:00<00:00, 3126.19 it/sec]
   INFO - 20:35:13:      4%|▍         | 4/100 [00:00<00:00, 3357.46 it/sec]
   INFO - 20:35:13:      5%|▌         | 5/100 [00:00<00:00, 3481.33 it/sec]
   INFO - 20:35:13:      6%|▌         | 6/100 [00:00<00:00, 3614.22 it/sec]
   INFO - 20:35:13:      7%|▋         | 7/100 [00:00<00:00, 3739.19 it/sec]
   INFO - 20:35:13:      8%|▊         | 8/100 [00:00<00:00, 3842.70 it/sec]
   INFO - 20:35:13:      9%|▉         | 9/100 [00:00<00:00, 3923.99 it/sec]
   INFO - 20:35:13:     10%|█         | 10/100 [00:00<00:00, 3936.47 it/sec]
   INFO - 20:35:13:     11%|█         | 11/100 [00:00<00:00, 3994.23 it/sec]
   INFO - 20:35:13:     12%|█▏        | 12/100 [00:00<00:00, 4058.35 it/sec]
   INFO - 20:35:13:     13%|█▎        | 13/100 [00:00<00:00, 4118.90 it/sec]
   INFO - 20:35:13:     14%|█▍        | 14/100 [00:00<00:00, 4128.83 it/sec]
   INFO - 20:35:13:     15%|█▌        | 15/100 [00:00<00:00, 4132.05 it/sec]
   INFO - 20:35:13:     16%|█▌        | 16/100 [00:00<00:00, 4163.60 it/sec]
   INFO - 20:35:13:     17%|█▋        | 17/100 [00:00<00:00, 4198.26 it/sec]
   INFO - 20:35:13:     18%|█▊        | 18/100 [00:00<00:00, 4234.53 it/sec]
   INFO - 20:35:13:     19%|█▉        | 19/100 [00:00<00:00, 4246.15 it/sec]
   INFO - 20:35:13:     20%|██        | 20/100 [00:00<00:00, 4270.53 it/sec]
   INFO - 20:35:13:     21%|██        | 21/100 [00:00<00:00, 4289.07 it/sec]
   INFO - 20:35:13:     22%|██▏       | 22/100 [00:00<00:00, 4313.72 it/sec]
   INFO - 20:35:13:     23%|██▎       | 23/100 [00:00<00:00, 4338.22 it/sec]
   INFO - 20:35:13:     24%|██▍       | 24/100 [00:00<00:00, 4346.80 it/sec]
   INFO - 20:35:13:     25%|██▌       | 25/100 [00:00<00:00, 4369.25 it/sec]
   INFO - 20:35:13:     26%|██▌       | 26/100 [00:00<00:00, 4393.18 it/sec]
   INFO - 20:35:13:     27%|██▋       | 27/100 [00:00<00:00, 4411.45 it/sec]
   INFO - 20:35:13:     28%|██▊       | 28/100 [00:00<00:00, 4427.71 it/sec]
   INFO - 20:35:13:     29%|██▉       | 29/100 [00:00<00:00, 4416.66 it/sec]
   INFO - 20:35:13:     30%|███       | 30/100 [00:00<00:00, 4433.26 it/sec]
   INFO - 20:35:13:     31%|███       | 31/100 [00:00<00:00, 4447.98 it/sec]
   INFO - 20:35:13:     32%|███▏      | 32/100 [00:00<00:00, 4354.47 it/sec]
   INFO - 20:35:13:     33%|███▎      | 33/100 [00:00<00:00, 4346.16 it/sec]
   INFO - 20:35:13:     34%|███▍      | 34/100 [00:00<00:00, 4359.18 it/sec]
   INFO - 20:35:13:     35%|███▌      | 35/100 [00:00<00:00, 4371.80 it/sec]
   INFO - 20:35:13:     36%|███▌      | 36/100 [00:00<00:00, 4386.58 it/sec]
   INFO - 20:35:13:     37%|███▋      | 37/100 [00:00<00:00, 4391.32 it/sec]
   INFO - 20:35:13:     38%|███▊      | 38/100 [00:00<00:00, 4401.64 it/sec]
   INFO - 20:35:13:     39%|███▉      | 39/100 [00:00<00:00, 4415.30 it/sec]
   INFO - 20:35:13:     40%|████      | 40/100 [00:00<00:00, 4428.81 it/sec]
   INFO - 20:35:13:     41%|████      | 41/100 [00:00<00:00, 4441.74 it/sec]
   INFO - 20:35:13:     42%|████▏     | 42/100 [00:00<00:00, 4444.91 it/sec]
   INFO - 20:35:13:     43%|████▎     | 43/100 [00:00<00:00, 4455.41 it/sec]
   INFO - 20:35:13:     44%|████▍     | 44/100 [00:00<00:00, 4467.86 it/sec]
   INFO - 20:35:13:     45%|████▌     | 45/100 [00:00<00:00, 4478.65 it/sec]
   INFO - 20:35:13:     46%|████▌     | 46/100 [00:00<00:00, 4490.58 it/sec]
   INFO - 20:35:13:     47%|████▋     | 47/100 [00:00<00:00, 4488.54 it/sec]
   INFO - 20:35:13:     48%|████▊     | 48/100 [00:00<00:00, 4496.91 it/sec]
   INFO - 20:35:13:     49%|████▉     | 49/100 [00:00<00:00, 4506.44 it/sec]
   INFO - 20:35:13:     50%|█████     | 50/100 [00:00<00:00, 4517.87 it/sec]
   INFO - 20:35:13:     51%|█████     | 51/100 [00:00<00:00, 4528.24 it/sec]
   INFO - 20:35:13:     52%|█████▏    | 52/100 [00:00<00:00, 4529.20 it/sec]
   INFO - 20:35:13:     53%|█████▎    | 53/100 [00:00<00:00, 4536.23 it/sec]
   INFO - 20:35:13:     54%|█████▍    | 54/100 [00:00<00:00, 4545.76 it/sec]
   INFO - 20:35:13:     55%|█████▌    | 55/100 [00:00<00:00, 4554.17 it/sec]
   INFO - 20:35:13:     56%|█████▌    | 56/100 [00:00<00:00, 4562.48 it/sec]
   INFO - 20:35:13:     57%|█████▋    | 57/100 [00:00<00:00, 4561.90 it/sec]
   INFO - 20:35:13:     58%|█████▊    | 58/100 [00:00<00:00, 4568.53 it/sec]
   INFO - 20:35:13:     59%|█████▉    | 59/100 [00:00<00:00, 4577.58 it/sec]
   INFO - 20:35:13:     60%|██████    | 60/100 [00:00<00:00, 4583.94 it/sec]
   INFO - 20:35:13:     61%|██████    | 61/100 [00:00<00:00, 4588.87 it/sec]
   INFO - 20:35:13:     62%|██████▏   | 62/100 [00:00<00:00, 4588.23 it/sec]
   INFO - 20:35:13:     63%|██████▎   | 63/100 [00:00<00:00, 4594.22 it/sec]
   INFO - 20:35:13:     64%|██████▍   | 64/100 [00:00<00:00, 4601.46 it/sec]
   INFO - 20:35:13:     65%|██████▌   | 65/100 [00:00<00:00, 4599.72 it/sec]
   INFO - 20:35:13:     66%|██████▌   | 66/100 [00:00<00:00, 4606.59 it/sec]
   INFO - 20:35:13:     67%|██████▋   | 67/100 [00:00<00:00, 4603.09 it/sec]
   INFO - 20:35:13:     68%|██████▊   | 68/100 [00:00<00:00, 4609.27 it/sec]
   INFO - 20:35:13:     69%|██████▉   | 69/100 [00:00<00:00, 4616.62 it/sec]
   INFO - 20:35:13:     70%|███████   | 70/100 [00:00<00:00, 4624.15 it/sec]
   INFO - 20:35:13:     71%|███████   | 71/100 [00:00<00:00, 4631.06 it/sec]
   INFO - 20:35:13:     72%|███████▏  | 72/100 [00:00<00:00, 4628.06 it/sec]
   INFO - 20:35:13:     73%|███████▎  | 73/100 [00:00<00:00, 4632.70 it/sec]
   INFO - 20:35:13:     74%|███████▍  | 74/100 [00:00<00:00, 4638.82 it/sec]
   INFO - 20:35:13:     75%|███████▌  | 75/100 [00:00<00:00, 4644.72 it/sec]
   INFO - 20:35:13:     76%|███████▌  | 76/100 [00:00<00:00, 4649.80 it/sec]
   INFO - 20:35:13:     77%|███████▋  | 77/100 [00:00<00:00, 4646.73 it/sec]
   INFO - 20:35:13:     78%|███████▊  | 78/100 [00:00<00:00, 4651.79 it/sec]
   INFO - 20:35:13:     79%|███████▉  | 79/100 [00:00<00:00, 4655.17 it/sec]
   INFO - 20:35:13:     80%|████████  | 80/100 [00:00<00:00, 4659.56 it/sec]
   INFO - 20:35:13:     81%|████████  | 81/100 [00:00<00:00, 4660.59 it/sec]
   INFO - 20:35:13:     82%|████████▏ | 82/100 [00:00<00:00, 4663.12 it/sec]
   INFO - 20:35:13:     83%|████████▎ | 83/100 [00:00<00:00, 4667.59 it/sec]
   INFO - 20:35:13:     84%|████████▍ | 84/100 [00:00<00:00, 4666.94 it/sec]
   INFO - 20:35:13:     85%|████████▌ | 85/100 [00:00<00:00, 4669.86 it/sec]
   INFO - 20:35:13:     86%|████████▌ | 86/100 [00:00<00:00, 4652.64 it/sec]
   INFO - 20:35:13:     87%|████████▋ | 87/100 [00:00<00:00, 4650.42 it/sec]
   INFO - 20:35:13:     88%|████████▊ | 88/100 [00:00<00:00, 4652.35 it/sec]
   INFO - 20:35:13:     89%|████████▉ | 89/100 [00:00<00:00, 4655.22 it/sec]
   INFO - 20:35:13:     90%|█████████ | 90/100 [00:00<00:00, 4658.32 it/sec]
   INFO - 20:35:13:     91%|█████████ | 91/100 [00:00<00:00, 4654.37 it/sec]
   INFO - 20:35:13:     92%|█████████▏| 92/100 [00:00<00:00, 4656.63 it/sec]
   INFO - 20:35:13:     93%|█████████▎| 93/100 [00:00<00:00, 4658.39 it/sec]
   INFO - 20:35:13:     94%|█████████▍| 94/100 [00:00<00:00, 4660.23 it/sec]
   INFO - 20:35:13:     95%|█████████▌| 95/100 [00:00<00:00, 4658.81 it/sec]
   INFO - 20:35:13:     96%|█████████▌| 96/100 [00:00<00:00, 4656.89 it/sec]
   INFO - 20:35:13:     97%|█████████▋| 97/100 [00:00<00:00, 4660.12 it/sec]
   INFO - 20:35:13:     98%|█████████▊| 98/100 [00:00<00:00, 4663.03 it/sec]
   INFO - 20:35:13:     99%|█████████▉| 99/100 [00:00<00:00, 4666.26 it/sec]
   INFO - 20:35:13:    100%|██████████| 100/100 [00:00<00:00, 4665.42 it/sec]
   INFO - 20:35:13: *** End Sampling execution ***

Settings#

Number of estimators#

The main hyperparameter of random forest regression is the number of trees in the forest (default: 100). Here is a comparison when increasing and decreasing this number:

model = create_regression_model(
    "RandomForestRegressor", training_dataset, n_estimators=10
)
model.learn()
predicted_output_data_1 = model.predict(input_data).ravel()
model = create_regression_model(
    "RandomForestRegressor", training_dataset, n_estimators=1000
)
model.learn()
predicted_output_data_2 = 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_1, label="Regression - 10 trees")
plt.plot(input_data.ravel(), predicted_output_data_2, label="Regression - 1000 trees")
plt.grid()
plt.legend()
plt.show()
plot random forest regression

Others#

The RandomForestRegressor class of scikit-learn has a lot of settings (read more), and we have chosen to exhibit only n_estimators. However, any argument of RandomForestRegressor can be set using the dictionary parameters. For example, we can impose a minimum of two samples per leaf:

model = create_regression_model(
    "RandomForestRegressor", training_dataset, parameters={"min_samples_leaf": 2}
)
model.learn()
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 - 2 samples")
plt.grid()
plt.legend()
plt.show()
plot random forest regression

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

Gallery generated by Sphinx-Gallery