Feature selection: sequential forward and backward
Sequential forward selection (SFS) and sequential backward selection (SBS) are greedy wrapper methods for feature selection that add or remove features one-by-one based on an objective (e.g., cross-validated accuracy) to find a useful subset without searching all subsets.
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Feature selection reduces the number of input variables (features) used by a machine learning model. Engineers use it to reduce model size, speed up training and inference, avoid overfitting, and improve interpretability — especially when data has many irrelevant or redundant features (for example in sensor arrays, text n-grams or high-dimensional embeddings).
Key ideas
Start from definitions:
- Feature: a single measurable property or attribute of an instance (also called variable or column). Example: temperature, pixel intensity.
- Feature subset: any collection of features chosen from the full set.
- Wrapper method: a feature selection approach that uses a specific learning algorithm (classifier or regressor) to evaluate feature subsets by their predictive performance.
- Greedy algorithm: an algorithm that makes the locally optimal choice at each step (without reconsidering earlier choices). SFS and SBS are greedy wrappers.
- Objective function: the metric used to evaluate a subset (for example, cross-validated accuracy, validation loss, AUC). Always state this before running selection.
Sequential Forward Selection (SFS) — exact statement and assumptions:
- SFS builds a feature subset by starting with the empty set and repeatedly adding the single feature that most improves the objective when combined with the features already selected.
- Assumptions: the objective function is computable for any subset; we accept greedy choices (no backtracking); we evaluate performance reliably (prefer cross-validation rather than training-set accuracy to reduce overfitting).
- Formal description (what to draw for an exam): draw a flow diagram with a box labelled “Selected set S (initially empty)”, an arrow to a loop labelled “for each feature f not in S evaluate score(S ∪ {f})”, arrow to “choose f* = argmax score(S ∪ {f})”, arrow to “update S ← S ∪ {f*}”, arrow to “stop when |S|=k or no improvement”.
Sequential Backward Selection (SBS) — exact statement and assumptions:
- SBS starts with the full set of features and repeatedly removes the single feature whose removal least decreases (or most improves) the objective when combined with the remaining features.
- Assumptions: same as SFS, but start from full feature vector; computational cost can be high when the full set is large.
- Diagram to draw: similar loop but start with S = all features, evaluate score(S \ {f}) for each f in S, remove the worst.
Important properties and derivations universities ask for:
- Why greedy is not optimal: Give a small counterexample (explain in words): two features together may be highly predictive but individually weak. A forward greedy method may skip them because neither individually improves the score; hence global optimum is not guaranteed.
- Number of subset evaluations: Let p be total features and k the target selected size (k ≤ p). For SFS the number of evaluations of candidate subsets equals: sum_{i=0}^{k-1} (p - i) = k·p - k·(k - 1)/2. Derive by: at iteration i (0-based) there are (p − i) unselected features to test; sum across k iterations gives formula above. For SBS removing until k features remain, number of evaluations equals: sum_{i=0}^{p-k-1} (p - i) = (p - k)·(p + (k + 1))/2 (you can algebraically simplify). Explain steps: at each removal step there are current |S| features; you evaluate removing each one, etc.
- Time complexity: if T_eval is the cost (time) to train and evaluate the classifier on one subset, SFS costs O((k·p - k(k - 1)/2)·T_eval) ≈ O(k·p·T_eval). SBS similar with p replacing k in leading term. State assumption: we count only subset evaluations; T_eval includes training and cross-validation.
- Stopping criteria: fixed number k, threshold on objective improvement (stop if best improvement < ε), or when removing/adding a feature degrades performance by more than allowed. Recommend cross-validation-based stopping to avoid overfitting to a validation set.
- Practical notes: use cross-validation for objective; scale features if the classifier is sensitive to scale; use wrappers with lighter base models if p is large; consider hybrid or embedded methods (e.g., regularisation, tree-based importance) if greedy wrappers are too slow. Use the same cross-validation splits across candidate subsets to ensure fair comparison, and use nested cross-validation or a held-out test set for an unbiased estimate of final performance after selection.
Formulas
Evaluations_SFS = sum_{i=0}^{k-1} (p - i) = k·p - k·(k - 1)/2 — p: total features (dimensionless), k: number of features to select (dimensionless). Applies when selecting exactly k features via SFS.
Evaluations_SBS = sum_{i=0}^{p-k-1} (p - i) = (p - k)·(p + k + 1)/2 — p and k as above. Applies when removing until k remain via SBS.
Time_SFS ≈ O(k·p·T_eval) — T_eval: time to train+validate the model on one subset (seconds). Applies as asymptotic cost for SFS.
Time_SBS ≈ O(((p - k)·(p + k + 1)/2)·T_eval) ≈ O(p^2·T_eval) — applies when removing until k remain; leading term is quadratic in p when k is not close to p.
Accuracy = (Number of correct predictions) / (Total number of samples) — no units. Use cross-validated accuracy as the objective where possible.
Stop if (best_improvement) < ε — ε: small positive threshold (dimensionless). Applies when using threshold stopping.
Worked examples
Example 1 — Sequential Forward Selection on a tiny binary problem (numerical steps): Data (6 samples): columns x1, x2, x3, label y. 1: 2, 1, 0, 1 2: 3, 0, 1, 1 3: 1, 1, 0, 0 4: 0, 0, 1, 0 5: 2, 0, 1, 1 6: 0, 1, 0, 0 Objective: use a simple threshold rule per feature: predict y=1 if xi >= 1; evaluate accuracy on the whole set (this is for a clear exam example — in practice use cross-validation). Step A: Evaluate single features.
- For x1: predictions (xi >=1) -> [1,1,1,0,1,0]; matches y in five out of six cases. Formula and numbers:
Accuracy_x1 = 5/6 = 0.8333. Final answer: 0.8333. - For x2: predictions -> [1,0,1,0,0,1]; correct 2/6.
Accuracy_x2 = 2/6 = 0.3333. Final answer: 0.3333. - For x3: predictions -> [0,1,0,1,1,0]; correct 4/6.
Accuracy_x3 = 4/6 = 0.6667. Final answer: 0.6667. Best single feature: x1 (0.8333). S ← {x1}. Step B: Try adding x2 or x3. Use a simple rule: predict y=1 if (x1 >= 1) OR (xj >= 1). - S ∪ {x2}: predictions become [1,1,1,0,1,1]; correct 4/6 =>
0.6667(worse). - S ∪ {x3}: predictions become [1,1,1,1,1,0]; correct 4/6 =>
0.6667(worse). Decision: adding x2 does not improve (tie) and x3 worsens, so SFS stops and returns {x1}. Final selected set: {x1}. Comment: this example shows SFS can stop early when no addition improves the chosen objective.
Example 2 — Sequential Backward Selection idea and complexity count (theoretical exercise):
Suppose p = 8 features and you want k = 3 features left. Number of subset evaluations by SBS:
Evaluations_SBS = sum_{i=0}^{8-3-1} (8 - i) = 8 + 7 + 6 + 5 + 4 = 30.
Show steps: start with 8 features, evaluate removing each of 8 → 8 evaluations; remove one → 7 remain → evaluate each removal of 7 → 7 evaluations; continue until 3 remain (that is 8+7+6+5+4 = 30). This is how many model trainings/validations are needed (multiply by T_eval to get time).
Common mistakes
- Expecting greedy methods to find the global best subset — they can miss combinations of weak-but-complementary features.
- Not accounting for correlated (redundant) features. Greedy forward can pick one of many correlated features and miss that combinations of the alternatives might be equally good.
- Underestimating cost: forgetting that each candidate subset requires training and validation — runtime grows quickly with p.
- Inconsistent objective: mixing different metrics mid-selection (always fix the metric and evaluation protocol beforehand).
For your exam
Types of questions you will see:
- Definitions: define SFS, SBS, wrapper method, greedy algorithm.
- Short notes: pros and cons; when to use forward vs backward.
- Derivations: derive the formula for number of evaluations for SFS or SBS (show summation steps).
- Numericals: run SFS or SBS on a tiny dataset and compute which features are selected (show steps and metrics).
- Diagrams: draw the loop/flow for SFS or SBS and label S, candidates, and stopping condition. What full-marks answers include:
- Clear definitions with bolded terms (or underlined in handwriting), stated assumptions (e.g., objective uses cross-validation), the correct summation and algebraic simplification for evaluation counts, a short example or clear explanation of stopping criteria, and mentions of practical concerns (compute cost, redundancy, overfitting).
Quick check
- In SFS you start with which set? 2) Give the formula for the number of candidate evaluations when SFS selects k features from p features. 3) Why can SFS miss the globally best feature subset?
Answers: 1) the empty set. 2)
k·p - k·(k - 1)/2. 3) Because it is greedy: it chooses the best immediate improvement at each step and does not revisit earlier choices, so it can miss combinations where features are useful only together.
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?