Polynomial Degree Validation Curve: Find the Best Degree and the Overfitting Onset

Quick Overview

Write NumPy code that sweeps polynomial degree on a fixed train and validation split, records both mean squared errors, and reports the best degree and the first degree where overfitting starts. It tests least-squares fitting with Vandermonde matrices, tie handling and reading the bias-variance tradeoff from a validation curve.

Polynomial Degree Validation Curve: Find the Best Degree and the Overfitting Onset

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `polynomial_validation_curve(x_train, y_train, x_val, y_val, degrees, tol=0.0)`. For each degree, fit a least-squares polynomial (all powers 0..degree) on the 1-D training data and record train and validation MSE. Also implement `locate_capacity(degrees, train_mse, val_mse, tol=0.0)`, which returns `(best_degree, overfit_degree)`. Requirements: - Return a dict with keys `degrees` (int array), `train_mse`, `val_mse`, `gap` (`val_mse - train_mse`), `best_degree`, `overfit_degree`. - `best_degree`: degree with minimum validation MSE, ties (within `1e-12`) go to the smallest degree. - `overfit_degree`: first `degrees[i]` where `val[i] - val[i-1] > tol` and `train[i] <= train[i-1] + 1e-9`, else `None`. - Both functions raise `ValueError` when `degrees` is empty, non-integer, negative or not strictly increasing. - Mismatched x/y or curve lengths, or a training set with no more points than the largest degree, also raise `ValueError`. Example: ```python locate_capacity([1, 2, 3, 4, 5], train_mse=[4.0, 2.0, 1.0, 0.5, 0.25], val_mse=[5.0, 3.0, 2.0, 2.5, 1.5]) # -> (5, 4) # min val at 5, val first rises at 4 while train falls ``` ```hint Building the design matrix `np.vander` with `increasing=True` gives the columns in the order of the powers, and `np.linalg.lstsq` solves the fit without forming the normal equations. ``` ```hint Reading the curve Scan the degrees in order. For the best degree, compare each value against the minimum with the stated tolerance. For the overfit degree, compare each point only with the one before it. ``` ### Constraints & Assumptions - Only `numpy` is available. - `x_train`, `y_train`, `x_val` and `y_val` are 1-D; each x array has the same length as its y array. - Degree `0` is the constant (training-mean) predictor. - The training set must have more points than the largest degree in `degrees`. ### What a Strong Answer Covers - One least-squares fit per degree using every power from 0 to that degree, with train MSE on the training split and validation MSE from the same coefficients. - The best-degree rule with the `1e-12` tie tolerance resolving to the smallest degree. - The overfit-onset rule on adjacent degrees, and why it also requires training error to keep falling. - The same input validation in both functions, so `locate_capacity` is safe to call on its own. - Awareness that high-degree Vandermonde matrices are ill-conditioned and what that does to the curve. ### Follow-up Questions - How would you generalise this to sweep decision tree `max_depth`? - Why might a single train/validation split give a misleading optimum, and how would you fix it? - What numerical issues appear at high degrees, and how do you mitigate them?

Overview: Write NumPy code that sweeps polynomial degree on a fixed train and validation split, records both mean squared errors, and reports the best degree and the first degree where overfitting starts. It tests least-squares fitting with Vandermonde matrices, tie handling and reading the bias-variance tradeoff from a validation curve.

PracHub logo
General
Nov 25, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Implement polynomial_validation_curve(x_train, y_train, x_val, y_val, degrees, tol=0.0). For each degree, fit a least-squares polynomial (all powers 0..degree) on the 1-D training data and record train and validation MSE. Also implement locate_capacity(degrees, train_mse, val_mse, tol=0.0), which returns (best_degree, overfit_degree).

Requirements:

  • Return a dict with keys degrees (int array), train_mse , val_mse , gap ( val_mse - train_mse ), best_degree , overfit_degree .
  • best_degree : degree with minimum validation MSE, ties (within 1e-12 ) go to the smallest degree.
  • overfit_degree : first degrees[i] where val[i] - val[i-1] > tol and train[i] <= train[i-1] + 1e-9 , else None .
  • Both functions raise ValueError when degrees is empty, non-integer, negative or not strictly increasing.
  • Mismatched x/y or curve lengths, or a training set with no more points than the largest degree, also raise ValueError .

Example:

locate_capacity([1, 2, 3, 4, 5],
                train_mse=[4.0, 2.0, 1.0, 0.5, 0.25],
                val_mse=[5.0, 3.0, 2.0, 2.5, 1.5])
# -> (5, 4)  # min val at 5, val first rises at 4 while train falls

Constraints & Assumptions

  • Only numpy is available.
  • x_train , y_train , x_val and y_val are 1-D; each x array has the same length as its y array.
  • Degree 0 is the constant (training-mean) predictor.
  • The training set must have more points than the largest degree in degrees .

What a Strong Answer Covers Guidance

  • One least-squares fit per degree using every power from 0 to that degree, with train MSE on the training split and validation MSE from the same coefficients.
  • The best-degree rule with the 1e-12 tie tolerance resolving to the smallest degree.
  • The overfit-onset rule on adjacent degrees, and why it also requires training error to keep falling.
  • The same input validation in both functions, so locate_capacity is safe to call on its own.
  • Awareness that high-degree Vandermonde matrices are ill-conditioned and what that does to the curve.

Follow-up Questions Guidance

  • How would you generalise this to sweep decision tree max_depth ?
  • Why might a single train/validation split give a misleading optimum, and how would you fix it?
  • What numerical issues appear at high degrees, and how do you mitigate them?