# Quick Start Guide: Optimal Regression Trees

This is a Python version of the corresponding OptimalTrees quick start guide.

In this example we will use Optimal Regression Trees (ORT) on the yacht hydrodynamics dataset. First we load in the data and split into training and test datasets:

import pandas as pd
"yacht_hydrodynamics.data",
delim_whitespace=True,
names=['position', 'prismatic', 'length_displacement', 'beam_draught',
'length_beam', 'froude', 'resistance'],
)

     position  prismatic  length_displacement  ...  length_beam  froude  resistance
0        -2.3      0.568                 4.78  ...         3.17   0.125        0.11
1        -2.3      0.568                 4.78  ...         3.17   0.150        0.27
2        -2.3      0.568                 4.78  ...         3.17   0.175        0.47
3        -2.3      0.568                 4.78  ...         3.17   0.200        0.78
4        -2.3      0.568                 4.78  ...         3.17   0.225        1.18
5        -2.3      0.568                 4.78  ...         3.17   0.250        1.82
6        -2.3      0.568                 4.78  ...         3.17   0.275        2.61
..        ...        ...                  ...  ...          ...     ...         ...
301      -2.3      0.600                 4.34  ...         2.73   0.300        4.15
302      -2.3      0.600                 4.34  ...         2.73   0.325        6.00
303      -2.3      0.600                 4.34  ...         2.73   0.350        8.47
304      -2.3      0.600                 4.34  ...         2.73   0.375       12.27
305      -2.3      0.600                 4.34  ...         2.73   0.400       19.59
306      -2.3      0.600                 4.34  ...         2.73   0.425       30.48
307      -2.3      0.600                 4.34  ...         2.73   0.450       46.66

[308 rows x 7 columns]
from interpretableai import iai
X = df.iloc[:, 0:-1]
y = df.iloc[:, -1]
(train_X, train_y), (test_X, test_y) = iai.split_data('regression', X, y,
seed=1)


### Optimal Regression Trees

We will use a GridSearch to fit an OptimalTreeRegressor:

grid = iai.GridSearch(
iai.OptimalTreeRegressor(
random_seed=1,
),
max_depth=range(1, 6),
)
grid.fit(train_X, train_y)
grid.get_learner()

Optimal Trees Visualization

We can make predictions on new data using predict:

grid.predict(test_X)

array([ 0.59223684,  2.2875    , 22.0675    ,  0.59223684,  0.59223684,
4.67392857, 57.5       ,  0.59223684,  0.59223684, 12.98      ,
4.67392857,  8.08066667, 12.98      ,  0.59223684,  2.2875    ,
4.67392857,  8.08066667, 22.0675    , 33.55769231, 48.39222222,
0.59223684,  0.59223684,  4.67392857,  4.67392857, 33.55769231,
2.2875    ,  4.67392857,  4.67392857,  8.08066667,  0.59223684,
4.67392857,  4.67392857,  0.59223684,  2.2875    ,  2.2875    ,
4.67392857, 33.55769231,  0.59223684,  0.59223684,  2.2875    ,
12.98      , 22.0675    ,  0.59223684,  2.2875    ,  4.67392857,
22.0675    , 48.39222222,  0.59223684,  0.59223684,  0.59223684,
2.2875    ,  0.59223684,  2.2875    ,  2.2875    ,  4.67392857,
4.67392857,  0.59223684,  0.59223684,  2.2875    ,  4.67392857,
8.08066667, 22.0675    ,  0.59223684,  2.2875    , 12.98      ,
39.55      ,  0.59223684,  0.59223684,  4.67392857, 12.98      ,
0.59223684,  0.59223684,  0.59223684,  2.2875    ,  8.08066667,
22.0675    ,  0.59223684,  0.59223684,  2.2875    ,  0.59223684,
0.59223684,  0.59223684,  2.2875    ,  8.08066667,  0.59223684,
0.59223684,  0.59223684,  8.08066667, 33.55769231,  0.59223684,
2.2875    ,  4.67392857])

We can evaluate the quality of the tree using score with any of the supported loss functions. For example, the $R^2$ on the training set:

grid.score(train_X, train_y, criterion='mse')

0.9941445278755715

Or on the test set:

grid.score(test_X, test_y, criterion='mse')

0.9917006908856699

### Optimal Regression Trees with Hyperplanes

To use Optimal Regression Trees with hyperplane splits (ORT-H), you should set the hyperplane_config parameter:

grid = iai.GridSearch(
iai.OptimalTreeRegressor(
random_seed=1,
hyperplane_config={'sparsity': 'all'},
),
max_depth=range(1, 5),
)
grid.fit(train_X, train_y)
grid.get_learner()

Optimal Trees Visualization

Now we can find the performance on the test set with hyperplanes:

grid.score(test_X, test_y, criterion='mse')

0.9877064480736487

It looks like the addition of hyperplane splits did not help too much here. It seems that the main variable affecting the target is froude, and so perhaps allowing multiple variables per split in the tree is not that useful for this dataset.

### Optimal Regression Trees with Linear Predictions

To use Optimal Regression Trees with linear regression in the leaves (ORT-L), you should set the regression_sparsity parameter to :all and use the regression_lambda parameter to control the degree of regularization.

grid = iai.GridSearch(
iai.OptimalTreeRegressor(
random_seed=1,
max_depth=2,
regression_sparsity='all',
),
regression_lambda=[0.0005, 0.001, 0.005],
)
grid.fit(train_X, train_y)
grid.get_learner()

Optimal Trees Visualization