Parameter Tuning

Parameter Tuning

When trying to select the right imputation model for a dataset, there are two considerations: which method to use for imputation and how to tune its parameters.

We recommend using the GridSearch to select both the method and parameters used. The easiest way to do this is to validate over multiple parameter grids, with one grid for each method under consideration that contains the parameters to tune for this method. The following example constructs a grid search with multiple parameter grids to select between the three optimal imputation method, while tuning the parameters knn_k and svm_c for the appropriate methods:

grid = IAI.GridSearch(
    IAI.ImputationLearner(
        random_seed=1,
    ),
    [
        (method=:opt_knn, knn_k=[5, 10, 15]),
        (method=:opt_svm, svm_c=[0.05, 0.1]),
        (method=:opt_tree,),
    ],
)
GridSearch - Unfitted OptKNNImputationLearner:
  random_seed: 1

GridSearch Params:
  (method=opt_knn,knn_k=5,)
  (method=opt_knn,knn_k=10,)
  (method=opt_knn,knn_k=15,)
  (method=opt_svm,svm_c=0.05,)
  (method=opt_svm,svm_c=0.1,)
  (method=opt_tree,)

We can see that the parameter grids have been expanded into six options for the grid search to select between. We can now run the grid search to find the best method and parameter combination:

IAI.fit!(grid, df)
All Grid Results:

│ Row │ method   │ knn_k   │ svm_c    │ train_score │ valid_score │
│     │ Symbol   │ Int64⍰  │ Float64⍰ │ Float64     │ Float64     │
├─────┼──────────┼─────────┼──────────┼─────────────┼─────────────┤
│ 1   │ opt_knn  │ 5       │ missing  │ NaN         │ 0.152946    │
│ 2   │ opt_knn  │ 10      │ missing  │ NaN         │ 0.169916    │
│ 3   │ opt_knn  │ 15      │ missing  │ NaN         │ 0.164355    │
│ 4   │ opt_svm  │ missing │ 0.05     │ NaN         │ 0.170388    │
│ 5   │ opt_svm  │ missing │ 0.1      │ NaN         │ 0.162432    │
│ 6   │ opt_tree │ missing │ missing  │ NaN         │ 0.152971    │

│ Row │ rank_valid_score │
│     │ Int64            │
├─────┼──────────────────┤
│ 1   │ 1                │
│ 2   │ 5                │
│ 3   │ 4                │
│ 4   │ 6                │
│ 5   │ 3                │
│ 6   │ 2                │

Best Params:
  method => opt_knn
  knn_k => 5

Best Model - Fitted OptKNNImputationLearner

Note that for imputation comparisons, a lower validation score is better.

As with other uses of GridSearch, you can use fit_cv! in place of fit! in order to conduct cross-validation. You can also use fit_transform_cv! to combine the fit_cv! and transform steps.

Which parameters to tune?

In most cases, there are very few parameters that need to be tuned for imputation methods. We recommend simply tuning over the different imputation methods and tuning knn_k and svm_c where appropriate, as shown in the example above.

The only other decision that needs to be made in most cases is whether to use the clustering approach. If the imputation is taking too long, you might like to enable this option to see the effect on imputation time.