Lesson 5.2 · 7 min read · about 45 min to master at a very slow pace

Model Complexity and Feature Selection

Complexity has many dials: number of features, coefficient size, depth, K. Why adding features always helps in-sample, why it eventually hurts out-of-sample, and how feature selection fits in.

Not startedJump to quiz

Many dials, one idea

ModelComplexity dialMore complex when...
Linear regressionNumber of predictors; size of coefficientsMore variables, bigger
Polynomial regressionDegreeHigher degree
K-nearest neighboursSmaller (more local, wigglier)
Decision treeDepthDeeper
K-meansNumber of clusters Larger
SVMCost ; kernel bandwidthLarger

More features: in-sample vs out-of-sample

0.511.5024681012number of variables in the modelerrorbest ≈ 8–9 variables
In-sample (training) errorOut-of-sample error
In-sample, adding a variable can only help. Out-of-sample, the best performance comes at a moderate number of variables; beyond that, the model overfits.

Adding a predictor to OLS can never increase training RSS: at worst its coefficient is set to zero. So training error always prefers the biggest model. Choosing which features to keep must use validation data, cross-validation, or a criterion that penalises size (adjusted , AIC/BIC).

Ways to choose features

  • Domain knowledge: fine when you genuinely know what's relevant.
  • Subset selection: best subset, forward or backward stepwise (5.3).
  • Shrinkage that selects: LASSO sets some coefficients exactly to zero (5.5).

The p-value trap

"We removed every variable with , refitted, and now all ." That's not a sound selection procedure. It ignores the uncertainty of the selection itself, makes the final p-values over-confident (they're reported as if no selection happened), and targets in-sample significance rather than out-of-sample prediction. Use CV-based stepwise selection or LASSO.

And the leakage trap

Feature selection is a learning step. It must happen inside each CV training fold, never on the full dataset first (lesson 2.8).

Cheatsheet

Select by p-values

Dropping and refitting ignores selection uncertainty and over-states significance. Use CV stepwise or LASSO.

Complexity dials

#features, |β|, degree, tree depth, small K (K-NN), large K (K-means), large C (SVM).

See every cheatsheet by category →

Check your understanding

Score 75% or more to master this lesson and earn XP. Answer first, then check.

Q1conceptWhich change makes a model MORE complex?
Q2conceptWhy can't training RSS be used to decide how many predictors to keep?
Q3diagnose"We dropped every variable with , refitted, and all the remaining p-values are below 0.05." What's the main problem?
Q4diagnoseA team picks the 30 features most correlated with the target using all the data, then uses CV to choose a model. Is that sound?
Q5interpretAs more variables are added, in-sample error keeps falling but out-of-sample error is lowest at about 8 variables. What does that show?