Dummy-Variable Trap as Rank Deficiency

Quick Overview

Build two regression design matrices from a three-level categorical column, one with an intercept and every dummy and one with a level dropped, then report each matrix's column count and numerical rank. Tests whether you can explain the dummy-variable trap as a linear dependency and what it means for OLS coefficients.

Dummy-Variable Trap as Rank Deficiency

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `def dummy_trap_ranks(df: pd.DataFrame) -> tuple`. `df` has one object column `g` over `n` rows with exactly 3 levels `'a'`, `'b'`, `'c'` (each appears at least once). Build two design matrices, each with a leading intercept column of ones, and return their column counts and numerical ranks. Requirements: - Matrix A: the intercept plus all 3 dummies from `pd.get_dummies(df['g'], dtype=float)`, shape `(n, 4)`. - Matrix B: the intercept plus k-1 dummies from `pd.get_dummies(df['g'], drop_first=True, dtype=float)`, shape `(n, 3)`. - Compute the ranks with `np.linalg.matrix_rank`. - Return `(ncols_A, rank_A, ncols_B, rank_B)` as Python `int`s (not `np.int64`). Example: ```python df = pd.DataFrame({"g": ["a", "b", "c", "a", "b"]}) dummy_trap_ranks(df) # -> (4, 3, 3, 3) ``` ```hint Add up the dummy columns For any row of matrix A, look at what the three dummy columns sum to, and compare that with the intercept column. ``` ### Constraints & Assumptions - `df` has a single column `g` of object dtype. - `g` takes exactly the levels `'a'`, `'b'` and `'c'`, and each level appears at least once, so `n >= 3`. - Both matrices must start with the intercept column of ones. ### What a Strong Answer Covers - Builds both matrices with the exact `pd.get_dummies` calls the task names and the intercept as the first column. - Returns the four values in the stated order, cast to Python `int`. - Explains why matrix A has fewer independent columns than columns, naming the exact linear dependency. - Explains why matrix B is full rank and what full rank means for whether the OLS coefficients are unique. - Notes how the result depends on every level being present. ### Follow-up Questions - Why does keeping the intercept but dropping one dummy still capture every group mean? - What happens numerically if you fit matrix A with `np.linalg.lstsq` instead of solving the normal equations? - What changes if you drop the intercept and keep all three dummies? - If `g` were a pandas categorical with a fourth declared level that never appears, how would the column counts and ranks change?

Overview: Build two regression design matrices from a three-level categorical column, one with an intercept and every dummy and one with a level dropped, then report each matrix's column count and numerical rank. Tests whether you can explain the dummy-variable trap as a linear dependency and what it means for OLS coefficients.

Nov 25, 2026
easyData ScientistOtherMachine Learning
0
0

Implement def dummy_trap_ranks(df: pd.DataFrame) -> tuple. df has one object column g over n rows with exactly 3 levels 'a', 'b', 'c' (each appears at least once). Build two design matrices, each with a leading intercept column of ones, and return their column counts and numerical ranks.

Requirements:

  • Matrix A: the intercept plus all 3 dummies from pd.get_dummies(df['g'], dtype=float) , shape (n, 4) .
  • Matrix B: the intercept plus k-1 dummies from pd.get_dummies(df['g'], drop_first=True, dtype=float) , shape (n, 3) .
  • Compute the ranks with np.linalg.matrix_rank .
  • Return (ncols_A, rank_A, ncols_B, rank_B) as Python int s (not np.int64 ).

Example:

df = pd.DataFrame({"g": ["a", "b", "c", "a", "b"]})
dummy_trap_ranks(df)  # -> (4, 3, 3, 3)

Constraints & Assumptions

  • df has a single column g of object dtype.
  • g takes exactly the levels 'a' , 'b' and 'c' , and each level appears at least once, so n >= 3 .
  • Both matrices must start with the intercept column of ones.

What a Strong Answer Covers Guidance

  • Builds both matrices with the exact pd.get_dummies calls the task names and the intercept as the first column.
  • Returns the four values in the stated order, cast to Python int .
  • Explains why matrix A has fewer independent columns than columns, naming the exact linear dependency.
  • Explains why matrix B is full rank and what full rank means for whether the OLS coefficients are unique.
  • Notes how the result depends on every level being present.

Follow-up Questions Guidance

  • Why does keeping the intercept but dropping one dummy still capture every group mean?
  • What happens numerically if you fit matrix A with np.linalg.lstsq instead of solving the normal equations?
  • What changes if you drop the intercept and keep all three dummies?
  • If g were a pandas categorical with a fourth declared level that never appears, how would the column counts and ranks change?