Compute the Gram matrix and moment vector for linear regression

Quick Overview

Implement a NumPy function that adds an intercept column to a feature matrix and returns the Gram matrix and moment vector of the linear regression normal equations. It tests design matrix construction, array shapes, and how ordinary least squares is set up before the solve.

Compute the Gram matrix and moment vector for linear regression

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement a NumPy function that returns the two building blocks of the ordinary least squares normal equations, `XtX @ beta = Xty`, without solving for `beta`. ```python def gram_and_moment(X: np.ndarray, y: np.ndarray) -> tuple[np.ndarray, np.ndarray]: ... ``` `X` is a float64 array of shape `(n, p)` with no ones column, and `y` is a float64 array of shape `(n,)`. The function should: - Build `Xd` by prepending a ones column to `X` (shape `(n, p+1)`, index `0` is the intercept). - Compute `XtX = Xd.T @ Xd` of shape `(p+1, p+1)`. - Compute `Xty = Xd.T @ y` of shape `(p+1,)`. - Return the plain tuple `(XtX, Xty)` in that order. Example: ```python X = np.array([[1.0], [2.0], [3.0]]) # shape (3, 1) y = np.array([2.0, 4.0, 6.0]) XtX, Xty = gram_and_moment(X, y) # Xd = [[1, 1], [1, 2], [1, 3]] # XtX = [[3, 6], [6, 14]] # Xty = [12, 28] ``` ```hint Shape of the ones column `np.hstack` needs both pieces to be 2-D. Check the shape of the ones array you build, and check that each product comes out with the shape listed above. ``` ### Constraints & Assumptions - `X` has shape `(n, p)` and does not include an intercept column. - `y` has shape `(n,)`. - Both inputs are float64. - Return the two products only. Do not call a solver or form an inverse. ### What a Strong Answer Covers - Building the design matrix with the ones column first, so index `0` of `beta` is the intercept. - Computing both products with matrix multiplication and returning them in the stated order and shapes. - What the entries of `XtX` and `Xty` mean and why they are the two sides of the normal equations. - The time and memory cost of forming `XtX`. - Inputs that make `XtX` singular, such as fewer rows than columns or a constant feature. ### Follow-up Questions - Given `XtX` and `Xty`, how would you finish solving for `beta`? - Why can `XtX` be ill-conditioned, and what would you do instead? - How would you compute `XtX` and `Xty` when `X` has too many rows to fit in memory?

Overview: Implement a NumPy function that adds an intercept column to a feature matrix and returns the Gram matrix and moment vector of the linear regression normal equations. It tests design matrix construction, array shapes, and how ordinary least squares is set up before the solve.

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

Implement a NumPy function that returns the two building blocks of the ordinary least squares normal equations, XtX @ beta = Xty, without solving for beta.

def gram_and_moment(X: np.ndarray, y: np.ndarray) -> tuple[np.ndarray, np.ndarray]:
    ...

X is a float64 array of shape (n, p) with no ones column, and y is a float64 array of shape (n,). The function should:

  • Build Xd by prepending a ones column to X (shape (n, p+1) , index 0 is the intercept).
  • Compute XtX = Xd.T @ Xd of shape (p+1, p+1) .
  • Compute Xty = Xd.T @ y of shape (p+1,) .
  • Return the plain tuple (XtX, Xty) in that order.

Example:

X = np.array([[1.0], [2.0], [3.0]])   # shape (3, 1)
y = np.array([2.0, 4.0, 6.0])

XtX, Xty = gram_and_moment(X, y)
# Xd  = [[1, 1], [1, 2], [1, 3]]
# XtX = [[3, 6], [6, 14]]
# Xty = [12, 28]

Constraints & Assumptions

  • X has shape (n, p) and does not include an intercept column.
  • y has shape (n,) .
  • Both inputs are float64.
  • Return the two products only. Do not call a solver or form an inverse.

What a Strong Answer Covers Guidance

  • Building the design matrix with the ones column first, so index 0 of beta is the intercept.
  • Computing both products with matrix multiplication and returning them in the stated order and shapes.
  • What the entries of XtX and Xty mean and why they are the two sides of the normal equations.
  • The time and memory cost of forming XtX .
  • Inputs that make XtX singular, such as fewer rows than columns or a constant feature.

Follow-up Questions Guidance

  • Given XtX and Xty , how would you finish solving for beta ?
  • Why can XtX be ill-conditioned, and what would you do instead?
  • How would you compute XtX and Xty when X has too many rows to fit in memory?