Classification

Quick Start Guide: Optimal Feature Selection for Classification

This is an R version of the corresponding OptimalFeatureSelection quick start guide.

In this example we will use Optimal Feature Selection on the Mushroom dataset, where the goal is to distinguish poisonous from edible mushrooms.

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

df <- read.table(
    "agaricus-lepiota.data",
    sep = ",",
    col.names = c("target", "cap_shape", "cap_surface", "cap_color",
                  "bruises", "odor", "gill_attachment", "gill_spacing",
                  "gill_size", "gill_color", "stalk_shape", "stalk_root",
                  "stalk_surface_above", "stalk_surface_below",
                  "stalk_color_above", "stalk_color_below", "veil_type",
                  "veil_color", "ring_number", "ring_type", "spore_color",
                  "population", "habitat"),
)
  target cap_shape cap_surface cap_color bruises odor gill_attachment
1      p         x           s         n       t    p               f
2      e         x           s         y       t    a               f
  gill_spacing gill_size gill_color stalk_shape stalk_root stalk_surface_above
1            c         n          k           e          e                   s
2            c         b          k           e          c                   s
  stalk_surface_below stalk_color_above stalk_color_below veil_type veil_color
1                   s                 w                 w         p          w
2                   s                 w                 w         p          w
  ring_number ring_type spore_color population habitat
1           o         p           k          s       u
2           o         p           n          n       g
 [ reached 'max' / getOption("max.print") -- omitted 8122 rows ]
iai::iai_setup()
X <- df[, 2:23]
y <- df[, 1]
split <- iai::split_data("classification", X, y, seed=1)
train_X <- split$train$X
train_y <- split$train$y
test_X <- split$test$X
test_y <- split$test$y

Model Fitting

We will use a grid_search to fit an optimal_feature_selection_classifier:

grid <- iai::grid_search(
    iai::optimal_feature_selection_classifier(
        random_seed=1,
    ),
    sparsity=1:10,
)
iai::fit(grid, train_X, train_y, validation_criterion="auc")
Julia Object of type GridSearch{OptimalFeatureSelectionClassifier,IAIBase.NullGridResult}.
All Grid Results:

│ Row │ sparsity │ train_score │ valid_score │ rank_valid_score │
│     │ Int64    │ Float64     │ Float64     │ Int64            │
├─────┼──────────┼─────────────┼─────────────┼──────────────────┤
│ 1   │ 1        │ 0.507063    │ 0.892471    │ 10               │
│ 2   │ 2        │ 0.616228    │ 0.945726    │ 9                │
│ 3   │ 3        │ 0.711851    │ 0.969207    │ 8                │
│ 4   │ 4        │ 0.764492    │ 0.977629    │ 7                │
│ 5   │ 5        │ 0.802974    │ 0.981979    │ 6                │
│ 6   │ 6        │ 0.82443     │ 0.984656    │ 5                │
│ 7   │ 7        │ 0.877074    │ 0.997937    │ 3                │
│ 8   │ 8        │ 0.916796    │ 0.999686    │ 1                │
│ 9   │ 9        │ 0.874916    │ 0.995357    │ 4                │
│ 10  │ 10       │ 0.907388    │ 0.99862     │ 2                │

Best Params:
  sparsity => 8

Best Model - Fitted OptimalFeatureSelectionClassifier:
  Constant: 1.35677
  Weights:
    gill_color==b:   1.24686
    gill_size==b:   -1.20474
    gill_size==n:    1.20474
    odor==a:        -3.81151
    odor==f:         2.98149
    odor==l:        -3.81851
    odor==n:        -3.70758
    spore_color==r:  5.98771

The model selected a sparsity of 8 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 <- iai::get_grid_results(grid)
plot(results$sparsity, results$valid_score, type="l", xlab="Sparsity",
     ylab="Validation AUC")

We see that the quality of the model quickly increases with additional terms to reach AUC 0.98 with four terms. After this, additional terms increase the quality more slowly, reaching AUC close to 1 with 7 terms. 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:

iai::predict(grid, test_X)
 [1] "e" "e" "e" "p" "e" "e" "e" "e" "e" "e" "p" "e" "e" "e" "e" "e" "e" "p" "e"
[20] "e" "e" "e" "e" "e" "e" "e" "e" "e" "e" "e" "e" "e" "e" "e" "e" "e" "e" "e"
[39] "e" "e" "e" "e" "e" "e" "e" "e" "e" "e" "e" "e" "e" "e" "e" "e" "e" "e" "e"
[58] "e" "e" "e"
 [ reached getOption("max.print") -- omitted 2377 entries ]

We can evaluate the quality of the model using score with any of the supported loss functions. For example, the misclassification on the training set:

iai::score(grid, train_X, train_y, criterion="misclassification")
[1] 0.9934939

Or the AUC on the test set:

iai::score(grid, test_X, test_y, criterion="auc")
[1] 0.9997997