# Quick Start Guide: Optimal Feature Selection for Regression

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:

using CSV, DataFrames
df = CSV.read("ailerons.csv", DataFrame)
13750×41 DataFrame
Row │ climbRate  Sgz    p        q        curPitch  curRoll  absRoll  diffC ⋯
│ Int64      Int64  Float64  Float64  Float64   Float64  Int64    Int64 ⋯
───────┼────────────────────────────────────────────────────────────────────────
1 │         2    -56    -0.33    -0.09      0.9       0.2      -11        ⋯
2 │       470    -39     0.02     0.12      0.39     -0.6      -12
3 │       165      4     0.14     0.14      0.78      0.4      -11
4 │      -113      5    -0.12     0.11      1.06      0.6      -10
5 │      -411    -21    -0.17     0.07      1.33     -0.6      -11        ⋯
6 │      -105    -42     0.23    -0.06      0.92     -0.6      -12
7 │       144    -40     0.31    -0.01      0.67      0.6      -10
8 │       249    -11    -0.38     0.06      0.56      0.4      -11
⋮   │     ⋮        ⋮       ⋮        ⋮        ⋮         ⋮        ⋮        ⋮  ⋱
13744 │      -224    -24    -0.22     0.0       0.97      0.5       -8        ⋯
13745 │      -204    -27    -0.25     0.01      0.95      0.3       -9
13746 │       399    -22     0.17     0.2       0.36     -0.2       -9
13747 │       237     -6     0.26     0.1       0.52      0.7       -8
13748 │      -148     -3    -0.37     0.09      0.89      0.7       -8        ⋯
13749 │      -237    -11    -0.47    -0.16      0.9      -0.4       -9
13750 │       128    -14    -0.07    -0.11      0.5      -1.2      -10
34 columns and 13735 rows omitted
X = df[:, 1:(end - 1)]
y = df.goal
(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=1:10,
)
IAI.fit!(grid, train_X, train_y)
All Grid Results:

Row │ sparsity  train_score  valid_score  rank_valid_score
│ Int64     Float64      Float64      Int64
─────┼──────────────────────────────────────────────────────
1 │        1     0.502496     0.469551                10
2 │        2     0.664859     0.661475                 9
3 │        3     0.75009      0.746062                 8
4 │        4     0.808994     0.800123                 7
5 │        5     0.814076     0.803629                 6
6 │        6     0.816877     0.807073                 5
7 │        7     0.819178     0.809386                 3
8 │        8     0.819249     0.809528                 2
9 │        9     0.819444     0.809719                 1
10 │       10     0.818511     0.808262                 4

Best Params:
sparsity => 9

Best Model - Fitted OptimalFeatureSelectionRegressor:
Constant: 0.000340372
Weights:
SeTime1:      -0.00724609
SeTime2:      -0.00917727
SeTime3:      -0.0092368
absRoll:       0.0000578331
curPitch:     -0.00000852542
curRoll:      -0.0000862067
diffClb:      -0.00000312272
diffRollRate:  0.00255865
p:            -0.000427066

The model selected a sparsity of 9 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:

using Plots
plot(grid, type=:validation)

We see that the quality of the model quickly increases with additional terms until we reach 4, and then only has 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 see the relative importance of the selected features with variable_importance:

IAI.variable_importance(IAI.get_learner(grid))
40×2 DataFrame
Row │ Feature       Importance
│ Symbol        Float64
─────┼──────────────────────────
1 │ absRoll       0.34228
2 │ p             0.185154
3 │ curRoll       0.119881
4 │ SeTime3       0.0914959
5 │ SeTime2       0.0909094
6 │ SeTime1       0.0714415
7 │ diffRollRate  0.048799
8 │ diffClb       0.0462209
⋮  │      ⋮            ⋮
34 │ diffSeTime4   0.0
35 │ diffSeTime5   0.0
36 │ diffSeTime6   0.0
37 │ diffSeTime7   0.0
38 │ diffSeTime8   0.0
39 │ diffSeTime9   0.0
40 │ q             0.0
25 rows omitted

We can also look at the feature importance across all sparsity levels:

plot(grid, type=:importance)

We can make predictions on new data using predict:

IAI.predict(grid, test_X)
4125-element Array{Float64,1}:
-0.001028137767
-0.001248730731
-0.001200336861
-0.000806856566
-0.001163303184
-0.000965276464
-0.000814835849
-0.000764116047
-0.000940686209
-0.000837538726
⋮
-0.000780658862
-0.000669674331
-0.000549805665
-0.000586365525
-0.000838101633
-0.001007202173
-0.001165584457
-0.000797824167
-0.000896424554

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:

IAI.score(grid, train_X, train_y, criterion=:mse)
0.816473302178

Or on the test set:

IAI.score(grid, test_X, test_y, criterion=:mse)
0.819417696263