Regression

Quick Start Guide: Optimal Feature Selection for Classification

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

In this example we will use Optimal Feature Selection on the Ailerons dataset, which addresses a control problem, namely flying a F16 aircraft. The attributes describe the status of the aeroplane, while the goal is to predict the control action on the ailerons of the aircraft.

First we load in the data and split into training and test datasets:

import pandas as pd
df = pd.read_csv("ailerons.csv")
       climbRate  Sgz     p     q  ...  diffSeTime14  alpha     Se    goal
0              2  -56 -0.33 -0.09  ...           0.0    0.9  0.032 -0.0009
1            470  -39  0.02  0.12  ...           0.0    0.9  0.034 -0.0011
2            165    4  0.14  0.14  ...           0.0    1.0  0.034 -0.0012
3           -113    5 -0.12  0.11  ...           0.0    0.9  0.033 -0.0011
4           -411  -21 -0.17  0.07  ...           0.0    0.9  0.032 -0.0008
5           -105  -42  0.23 -0.06  ...           0.0    0.8  0.028 -0.0010
6            144  -40  0.31 -0.01  ...           0.0    0.8  0.029 -0.0012
...          ...  ...   ...   ...  ...           ...    ...    ...     ...
13743       -224  -24 -0.22  0.00  ...           0.0    0.7  0.026 -0.0007
13744       -204  -27 -0.25  0.01  ...           0.0    0.7  0.026 -0.0006
13745        399  -22  0.17  0.20  ...           0.0    0.8  0.027 -0.0008
13746        237   -6  0.26  0.10  ...           0.0    0.8  0.027 -0.0010
13747       -148   -3 -0.37  0.09  ...           0.0    0.7  0.026 -0.0006
13748       -237  -11 -0.47 -0.16  ...           0.0    0.7  0.023 -0.0005
13749        128  -14 -0.07 -0.11  ...           0.0    0.6  0.022 -0.0006

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

Model Fitting

We will use a GridSearch to fit an OptimalFeatureSelectionRegressor:

grid = iai.GridSearch(
    iai.OptimalFeatureSelectionRegressor(
        random_seed=1,
    ),
    sparsity=range(1, 11),
)
grid.fit(train_X, train_y)
All Grid Results:

│ Row │ sparsity │ train_score │ valid_score │ rank_valid_score │
│     │ Int64    │ Float64     │ Float64     │ Int64            │
├─────┼──────────┼─────────────┼─────────────┼──────────────────┤
│ 1   │ 1        │ 0.496049    │ 0.482452    │ 10               │
│ 2   │ 2        │ 0.669033    │ 0.658196    │ 9                │
│ 3   │ 3        │ 0.753697    │ 0.735947    │ 8                │
│ 4   │ 4        │ 0.803797    │ 0.79921     │ 7                │
│ 5   │ 5        │ 0.811743    │ 0.809951    │ 4                │
│ 6   │ 6        │ 0.815567    │ 0.811869    │ 1                │
│ 7   │ 7        │ 0.815114    │ 0.808024    │ 6                │
│ 8   │ 8        │ 0.81547     │ 0.808385    │ 5                │
│ 9   │ 9        │ 0.817183    │ 0.811309    │ 3                │
│ 10  │ 10       │ 0.817397    │ 0.811351    │ 2                │

Best Params:
  sparsity => 6

Best Model - Fitted OptimalFeatureSelectionRegressor:
  Constant: 0.000340755
  Weights:
    SeTime1: -0.0113283
    SeTime2: -0.0149811
    absRoll:  0.0000570265
    curRoll: -0.000107629
    diffClb: -0.00000229311
    p:       -0.000406711

The model selected a sparsity of 6 as the best parameter, but we observe that the validation scores are close for many of the parameters. We can use the results of the grid search to explore the tradeoff between the complexity of the regression and the quality of predictions:

results = grid.get_grid_results()
ax = results.plot(x='sparsity', y='valid_score', legend=False)
ax.set_xlabel('Sparsity')
ax.set_ylabel('Validation R-Squared')

We see that the quality of the model quickly increases with additional terms until we reach 4, and then only small increases afterwards. Depending on the application, we might decide to choose a lower sparsity for the final model than the value chosen by the grid search.

We can make predictions on new data using predict:

grid.predict(test_X)
"""
array([-0.00104327, -0.00119998, -0.00109744, ..., -0.00077072,
       -0.00076907, -0.00055041])

We can evaluate the quality of the model 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.811925993877

Or on the test set:

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