Best-subset selection by adjusted R² over every column subset

Quick Overview

Implement exhaustive best-subset selection for a small linear regression: fit OLS with an intercept on every non-empty column subset and return the one with the highest adjusted R². It tests the adjusted R² penalty, deterministic tie-breaking, and how exhaustive search scales with the number of features.

Best-subset selection by adjusted R² over every column subset

Role: Data Scientist

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `best_subset_by_adjusted_r2(X, y)` for a feature matrix `X` of shape `(n, p) = (24, 4)` and a target `y`. Enumerate every non-empty subset of column indices, fit OLS with an intercept on each, and return the subset with the highest adjusted R². ```python def best_subset_by_adjusted_r2(X: np.ndarray, y: np.ndarray) -> tuple: ... ``` Requirements: - Evaluate all 15 non-empty subsets. Fit each one with an intercept, using only the columns in that subset. - Score each fit with `adj_R2 = 1 - (1 - R2) * (n - 1) / (n - k - 1)`, where `k = len(subset)` excludes the intercept and R² is the standard training-data OLS R². - Return `(best_cols, best_adj_r2)`, where `best_cols` is a tuple of column indices sorted ascending and `best_adj_r2` is a `float`. - Break ties by preferring fewer columns, then the lexicographically smallest sorted subset. You may use `numpy` (for example `np.linalg.lstsq`) or `statsmodels`. If you fit manually, add the intercept column yourself. ```hint Visiting order `itertools.combinations` over each size from 1 to `p` yields subsets by size, and in lexicographic order within a size. Think about how that order relates to the tie rules. ``` ```hint Near-equal scores Two subsets can have adjusted R² values that should be equal but differ in the last few bits, for example when two columns of `X` are identical. Decide how your comparison treats such near ties. ``` ### Constraints & Assumptions - `n = 24` rows and `p = 4` columns, so there are `2^4 - 1 = 15` non-empty subsets. - Every fit includes an intercept, and the intercept is not counted in `k`. - `k` ranges from 1 to 4, so `n - k - 1` ranges from 19 to 22. - `numpy` or `statsmodels` is available. ### What a Strong Answer Covers - Enumerates all 15 non-empty subsets and fits each with its own intercept-augmented design matrix. - Computes R² against the TSS around the mean of `y` and applies the penalty with `k` equal to the subset size. - Implements both tie rules and handles near-equal floating-point scores on purpose. - Explains why adjusted R² can choose a smaller subset when plain R² always favors the full model. - States the cost of exhaustive search and how it grows with `p`. ### Follow-up Questions - Why does adjusted R² penalize noise columns while plain R² does not? - How would this scale beyond a handful of columns, and what alternatives exist? - Is the winning adjusted R² a fair estimate of how the chosen model will do on new data?

Overview: Implement exhaustive best-subset selection for a small linear regression: fit OLS with an intercept on every non-empty column subset and return the one with the highest adjusted R². It tests the adjusted R² penalty, deterministic tie-breaking, and how exhaustive search scales with the number of features.

General logo
General
Nov 24, 2026
mediumData ScientistOtherMachine Learning
0
0

Implement best_subset_by_adjusted_r2(X, y) for a feature matrix X of shape (n, p) = (24, 4) and a target y. Enumerate every non-empty subset of column indices, fit OLS with an intercept on each, and return the subset with the highest adjusted R².

def best_subset_by_adjusted_r2(X: np.ndarray, y: np.ndarray) -> tuple:
    ...

Requirements:

  • Evaluate all 15 non-empty subsets. Fit each one with an intercept, using only the columns in that subset.
  • Score each fit with adj_R2 = 1 - (1 - R2) * (n - 1) / (n - k - 1) , where k = len(subset) excludes the intercept and R² is the standard training-data OLS R².
  • Return (best_cols, best_adj_r2) , where best_cols is a tuple of column indices sorted ascending and best_adj_r2 is a float .
  • Break ties by preferring fewer columns, then the lexicographically smallest sorted subset.

You may use numpy (for example np.linalg.lstsq) or statsmodels. If you fit manually, add the intercept column yourself.

Constraints & Assumptions

  • n = 24 rows and p = 4 columns, so there are 2^4 - 1 = 15 non-empty subsets.
  • Every fit includes an intercept, and the intercept is not counted in k .
  • k ranges from 1 to 4, so n - k - 1 ranges from 19 to 22.
  • numpy or statsmodels is available.

What a Strong Answer Covers Guidance

  • Enumerates all 15 non-empty subsets and fits each with its own intercept-augmented design matrix.
  • Computes R² against the TSS around the mean of y and applies the penalty with k equal to the subset size.
  • Implements both tie rules and handles near-equal floating-point scores on purpose.
  • Explains why adjusted R² can choose a smaller subset when plain R² always favors the full model.
  • States the cost of exhaustive search and how it grows with p .

Follow-up Questions Guidance

  • Why does adjusted R² penalize noise columns while plain R² does not?
  • How would this scale beyond a handful of columns, and what alternatives exist?
  • Is the winning adjusted R² a fair estimate of how the chosen model will do on new data?