Vectorized Linear Predictor for Logistic Regression

Quick Overview

Implement the vectorized linear predictor of a logistic regression, the log-odds score for every sample from a feature matrix, coefficient vector and intercept. Tests NumPy matrix-vector products, output shapes and broadcasting pitfalls, and how the score feeds the sigmoid.

Vectorized Linear Predictor for Logistic Regression

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `linear_score(X, w, b)` returning the logistic regression log-odds `X[i] @ w + b` for each sample. Inputs: a `float64` matrix `X` of shape `(n_samples, n_features)`, a `float64` vector `w` of shape `(n_features,)`, and a Python float `b`. Requirements: - Return a 1-D `float64` `np.ndarray` of shape `(n_samples,)`, not `(n_samples, 1)`. - Use a single matrix-vector product `X @ w + b`, not a Python loop. Example: ```python X = np.array([[1.0, 2.0], [3.0, 4.0], [-1.0, 0.5], [0.0, 0.0]]) w = np.array([0.5, -1.0]) b = 0.25 linear_score(X, w, b) # -> [-1.25, -2.25, -0.75, 0.25] ``` ```hint Shapes Work out the shape NumPy returns for a 2-D matrix times a 1-D vector, and compare it with what you get when the coefficients are stored as a column. ``` ### Constraints & Assumptions - `X` is a 2-D `float64` array of shape `(n_samples, n_features)`. - `w` is 1-D with shape `(n_features,)` and `b` is a scalar. - The output is 1-D with shape `(n_samples,)`. ### What a Strong Answer Covers - One vectorized product over all samples, with the scalar intercept added by broadcasting. - Why the result is 1-D and what goes wrong downstream with a column vector. - What the score means (log-odds) and how it turns into a probability. - The time and space cost. ### Follow-up Questions - Why return shape `(n_samples,)` instead of `(n_samples, 1)`? - How does this connect to predicting probabilities? - What changes if `X` is a SciPy sparse matrix?

Overview: Implement the vectorized linear predictor of a logistic regression, the log-odds score for every sample from a feature matrix, coefficient vector and intercept. Tests NumPy matrix-vector products, output shapes and broadcasting pitfalls, and how the score feeds the sigmoid.

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

Implement linear_score(X, w, b) returning the logistic regression log-odds X[i] @ w + b for each sample. Inputs: a float64 matrix X of shape (n_samples, n_features), a float64 vector w of shape (n_features,), and a Python float b.

Requirements:

  • Return a 1-D float64 np.ndarray of shape (n_samples,) , not (n_samples, 1) .
  • Use a single matrix-vector product X @ w + b , not a Python loop.

Example:

X = np.array([[1.0, 2.0], [3.0, 4.0], [-1.0, 0.5], [0.0, 0.0]])
w = np.array([0.5, -1.0])
b = 0.25
linear_score(X, w, b)  # -> [-1.25, -2.25, -0.75, 0.25]

Constraints & Assumptions

  • X is a 2-D float64 array of shape (n_samples, n_features) .
  • w is 1-D with shape (n_features,) and b is a scalar.
  • The output is 1-D with shape (n_samples,) .

What a Strong Answer Covers Guidance

  • One vectorized product over all samples, with the scalar intercept added by broadcasting.
  • Why the result is 1-D and what goes wrong downstream with a column vector.
  • What the score means (log-odds) and how it turns into a probability.
  • The time and space cost.

Follow-up Questions Guidance

  • Why return shape (n_samples,) instead of (n_samples, 1) ?
  • How does this connect to predicting probabilities?
  • What changes if X is a SciPy sparse matrix?