Feature selection

Intermediate

What you need first. Nothing else on this path comes first. This page is part of Statistical learning.

In statistical learning, feature selection is the choice of which measured predictors enter a fitted model, with the others left out. A regression that is handed every recorded measurement will give each of them a coefficient. Some of those measurements have nothing to do with the response. Feature selection, also called variable selection, is the decision to leave those measurements out, so that the fitted model uses only a subset of the predictors.[1] [2]

Feature selection

Feature selection
Fact Value
Topic Statistical learning
Listed on Statistical learning
Worked example 1

Motivating example

Three cases have predictors (x,z)(x,z) and response yy equal to (0,0,1)(0,0,1), (1,0,1)(1,0,1) and (2,1,3)(2,1,3). An intercept-only fit predicts the mean 5/35/3 every time, and its residual sum of squares is 8/38/3. A straight line in xx alone, still with an intercept, has residual sum of squares 2/32/3. The line 1+2z1+2z passes through all three points, so the residual sum of squares for zz alone is 00. The variable that earns its place is zz. Keeping xx as well cannot make the training residual sum of squares smaller than 00, and it adds a coefficient that the three points do not need.[1]

Definition

For the linear model

Y=β0+β1X1+⋯+βpXp+ε,Y=\beta_0+\beta_1 X_1+\cdots+\beta_p X_p+\varepsilon,

feature selection means choosing a subset of {X1,…,Xp}\{X_1,\ldots,X_p\} and fitting the model on that subset. The excluded predictors are absent. Their coefficients are not estimated, which is the same end state as a coefficient fixed at zero.[1] [2]

Subset selection does the choice first and then applies least squares to the survivors. Best-subset selection, for each size kk, finds the subset of kk predictors with the smallest residual sum of squares. Forward stepwise selection starts from the intercept and adds, one predictor at a time, the variable that most improves the fit. Backward stepwise selection starts from the full model and deletes the predictor with the least effect on the fit. Shrinkage can do the same job inside one fit: an ℓ1\ell_1 penalty, as in the lasso, drives some coefficients to exact zeros when the penalty is large enough, and those zeros are the excluded variables.[1] [2]

The same idea is not limited to least squares. For logistic regression the residual sum of squares is replaced by the deviance, and an ℓ1\ell_1 penalty on the logistic coefficients again sets some of them to zero. A quadratic penalty does not. Linear discriminant analysis, ordinary logistic regression, and the support-vector classifier, regularised by a square penalty, give every feature a nonzero weight.[1] [2]

Interpretation

Least squares with many predictors has two familiar failures. When the number of predictors is not small relative to the sample, the fitted coefficients move a lot from sample to sample, and predictions on new cases suffer. And a coefficient that is only a little different from zero is still a number one has to explain. Setting the useless coefficients to zero, or never estimating them, buys a simpler equation. The price is bias: a predictor that was wrongly dropped cannot help. The useful procedures are the ones that make that exchange on purpose, and that do not decide it by training error alone.[1] [2]

Training residual sum of squares for the best subset of each size cannot rise as the size increases: the larger search includes every smaller subset. On the three cases above, the best residual sums of squares are 8/38/3 at size 00, 00 at size 11, and 00 at size 22. Training R2R^2 moves the other way. A rule that picks the subset with the smallest training residual sum of squares therefore has no reason to stop before every predictor is available, and here it cannot prefer {z}\{z\} to {x,z}\{x,z\}.[1] [2]

The search itself has a cost that grows with pp. Best subset selection considers 2p2^p subsets. At p=20p=20 that is 1,048,5761{,}048{,}576 fits. Forward stepwise fits the null model and then p−kp-k candidates at step kk, which is 1+p(p+1)/21+p(p+1)/2 models in all, or 211211 models when p=20p=20. Forward stepwise can still miss the best subset of a given size. If the best single predictor is X1X_1 and the best pair is {X2,X3}\{X_2,X_3\}, the forward path has already committed to X1X_1 and cannot reach that pair. The narrower path is also the statistical point of the shortcut: a search through every subset pays in variance for the subset that looked best on the training sample.[1] [2]

Ridge regression shrinks coefficients and does not set them to zero, except in the limit of an infinite penalty, where every coefficient is zero. The lasso penalty λ∑j∣βj∣\lambda\sum_j|\beta_j| does set coefficients to zero for large enough λ\lambda, so the fitted equation names the variables it kept. When there are more predictors than observations, a lasso solution has at most as many nonzero coefficients as there are observations.[1] [2]

Use a selection procedure when the scientific or practical product is a short list of predictors, or when pp is large enough that an unpenalised least-squares fit is noisy. Best subset selection is feasible for pp up to about 3030 or 4040. Beyond that, forward stepwise, or the lasso, is the computation one can actually run. Backward stepwise needs more observations than predictors, because it starts from the full least-squares fit. Forward stepwise does not.[2] [1]

The size of the subset, or the lasso penalty, is not chosen by the training residual sum of squares. Validation error, cross-validation, CpC_p, AIC, BIC, or adjusted R2R^2 compare models that have different numbers of coefficients. For a least-squares fit with dd predictors, one estimate of test mean squared error is Cp=(RSS+2dσ^2)/nC_p=(\mathrm{RSS}+2d\hat\sigma^2)/n. The penalty term is what can make a larger subset look worse.[1]

Subset selection and shrinkage are the two standard organisations of this idea in linear regression. Dimensionality reduction is the third organisation in the same chapter, and it is not feature selection: every original predictor can still appear inside a principal component. The component may be a useful summary, but it does not drop a named variable from the equation.[1]

A smaller training residual sum of squares does not mean a better selected model. On the three cases, {z}\{z\} and {x,z}\{x,z\} both have training residual sum of squares 00. The second model is not more accurate on new cases merely because it contains an extra predictor. Training error is why the size of the subset is chosen by a validation score or by a penalty such as CpC_p, not by residual sum of squares itself.[1]

Forward stepwise is not best subset selection on a budget that happens to reach the same answer. It is a nested search. The three-case example happens to agree at size 11. The search disagrees as soon as the best pair does not contain the best single predictor.[1] [2]

Ridge regression is not a feature-selection method. Shrinking a coefficient is not the same as removing the predictor. The square penalty leaves every coefficient nonzero for any finite penalty. The lasso is the penalty that removes predictors.[1] [2]

The comparisons above use squared error, an intercept, and predictors that enter as they are. A monotone transformation of zz, or a penalty on coefficients that have not been put on a common scale, changes which variable looks useful. Best subset and stepwise selection, followed by ordinary least squares on the chosen subset, treat the selected variables as if they had been named in advance. The selection search is part of the fitting, and a later standard error that ignores the search is too small.[1] [2]

A large search overfits. With enough candidate predictors, some subset will look good on the training set and fail on new cases. That is why p=20p=20 is already described as a statistical problem for best subset selection, not only a computational one. Forward stepwise reduces the number of subsets, and it can still overfit the order in which variables were added.[1] [2]

Quadratic regularisation does not become feature selection by discarding small weights after the fit and refitting. That backward trick has been tried on a support-vector classifier with thousands of genes and a few hundred samples, and the accuracy fell as genes were removed. An ℓ1\ell_1 penalty builds the zeros into the fit instead.[2]

Worked example

Take the three cases (x,z,y)=(0,0,1)(x,z,y)=(0,0,1), (1,0,1)(1,0,1), (2,1,3)(2,1,3).

The sample mean of yy is (1+1+3)/3=5/3(1+1+3)/3=5/3. The intercept-only residual sum of squares is

2(1−53)2+(3−53)2=83.2\left(1-\frac53\right)^2+\left(3-\frac53\right)^2=\frac{8}{3}.

The least-squares line in xx is 2/3+x2/3+x. Its fitted values are 2/32/3, 5/35/3 and 8/38/3, and the residual sum of squares is

(13)2+(−23)2+(13)2=23.\left(\frac13\right)^2+\left(-\frac23\right)^2+\left(\frac13\right)^2=\frac{2}{3}.

The line 1+2z1+2z fits all three points, so its residual sum of squares is 00.

Best subset selection of size 11 therefore returns {z}\{z\}. Forward stepwise starts from the intercept, compares the drop from 8/38/3 to 2/32/3 against the drop from 8/38/3 to 00, and adds zz. The two-predictor fit also has residual sum of squares 00, because it can set the coefficient of xx to zero and keep 1+2z1+2z. Training residual sum of squares does not prefer the one-predictor model.

For a separate count, p=20p=20 predictors give 220=1,048,5762^{20}=1{,}048{,}576 best-subset models and 1+20⋅21/2=2111+20\cdot 21/2=211 forward-stepwise models.

Your turn. On these three cases, what is the training residual sum of squares of the intercept-only model, and of the least-squares model that uses only xx?

The intercept-only residual sum of squares is 8/38/3. The model that uses only xx has residual sum of squares 2/32/3.

Connections

Subset selection and shrinkage are the paired tools. The lasso and the logistic lasso are the shrinkage versions that set coefficients to zero. Ridge regression shrinks without selection. Dimensionality reduction replaces predictors by a few combinations rather than by a subset. Cross-validation is the usual way to choose how many variables, or how large a penalty, to keep. Overfitting is what a selection rule is trying not to do when it refuses the subset with the smallest training error.[1] [2]

Check yourself

Try these questions before opening an answer.

Intercept-only residual sum of squares

For the cases (x, z, y) = (0, 0, 1), (1, 0, 1) and (2, 1, 3), what is the residual sum of squares of the intercept-only fit?

The residual sum of squares is 8/3.

Predictor x alone

On the same three cases, what is the residual sum of squares of the least-squares line that uses only x and an intercept?

The residual sum of squares is 2/3.

How many best-subset models

Best-subset selection with 20 predictors fits how many models, and forward stepwise fits how many?

Best subset fits 1048576 models. Forward stepwise fits 211.

History

Subset search is the older organisation: all subsets of each size when pp is moderate, and a forward or backward path when it is not. The leaps-and-bounds algorithm made an exhaustive subset search practical for a few dozen predictors. Forward stepwise, a greedy nested path, and backward deletion of the weakest coefficient were the practical substitutes, with the warning that the path need not contain the best subset of each size.[2] [1]

The lasso made selection a property of one convex penalty rather than of a search over subsets. In high-dimensional classification the distinction became sharp: square penalties on discriminant analysis, logistic regression, and support-vector machines keep every feature, while an absolute penalty sets some coefficients to zero and so selects.[1] [2]

See also

Line chart · Apriori algorithm · Asymptotic analysis · Average treatment effect · Base rate · Bayesian inference · Bayesian model · Bayesian probability

Glossary and index

References

  1. ^ a b c d e f g h i j k l m n o p q r s t a. (Ch. 6, pp. 225–226) b. (Ch. 6, pp. 227–228) c. (Ch. 6, pp. 225–226) d. (Ch. 6, pp. 226–231, 241–242) e. (Ch. 6, p. 228) f. (Ch. 6, pp. 225–226) g. (Ch. 6, pp. 227–228) h. (Ch. 6, pp. 228–231) i. (Ch. 6, pp. 241–242) j. (Ch. 6, pp. 228–231) k. (Ch. 6, pp. 227–228, 233, (6.2)) l. (Ch. 6, p. 226) m. (Ch. 6, pp. 227–228) n. (Ch. 6, pp. 230–231) o. (Ch. 6, pp. 241–242) p. (Ch. 6, pp. 229–231) q. (Ch. 6, p. 229) r. (Ch. 6, pp. 226–228, 241–242) s. (Ch. 6, pp. 227–231) t. (Ch. 6, pp. 241–242) James, G., Witten, D., Hastie, T. & Tibshirani, R. (2021). An Introduction to Statistical Learning: with Applications in R (2nd ed.). Springer, New York. https://doi.org/10.1007/978-1-0716-1418-1
  2. ^ a b c d e f g h i j k l m n o p q a. (p. 57, §3.3) b. (p. 57, §3.3) c. (pp. 57–59, §3.3; p. 661, §18.4) d. (p. 658, §18.3.4; p. 661, §18.4) e. (p. 57, §3.3) f. (p. 57, §3.3.1) g. (pp. 58–59, §3.3.2) h. (p. 661, §18.4, (18.18)) i. (pp. 57–59, §3.3) j. (pp. 58–59) k. (pp. 658, 661) l. (pp. 58–59) m. (pp. 58–59) n. (p. 658, §18.3.4; p. 661, §18.4) o. (pp. 57–59, 658, 661) p. (pp. 57–59, §3.3) q. (p. 658, §18.3.4; p. 661, §18.4) Hastie, T., Tibshirani, R. & Friedman, J. (2009). The Elements of Statistical Learning. Springer. https://doi.org/10.1007/978-0-387-84858-7
Cite this page
Plain text
StatWiki (2026). "Feature selection." StatWiki. Last updated 2 December 2026. https://www.statwiki.io/articles/feature-selection.html
BibTeX
@misc{statwiki_feature_selection,
  author = {{StatWiki}},
  title = {{Feature selection}},
  year = {2026},
  url = {https://www.statwiki.io/articles/feature-selection.html},
  urldate = {2026-12-02},
  note = {StatWiki, last updated 2 December 2026}
}