Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Logistic Regression From Scratch

HardPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Logistic Regression From Scratch. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

CircleUp needs a lightweight binary classifier for scored company signals. Implement batch logistic regression from scratch, without ML libraries, using gradient descent.

Formal Specification

Implement train_logistic_regression(X, y, learning_rate, iterations, l2).

  • X is a list of n feature vectors, each containing d floats.
  • y is a list of n binary labels, where each value is 0 or 1.
  • learning_rate is the gradient descent step size.
  • iterations is the number of full-batch parameter updates.
  • l2 is the nonnegative L2 regularization strength.

Initialize the intercept and all weights to 0.0. For each iteration, compute predictions with the sigmoid function, calculate gradients over the complete dataset, apply L2 regularization to weights only, then update all parameters simultaneously. Return [intercept, weight_0, ..., weight_d-1].

Use a numerically stable sigmoid implementation so large positive or negative scores do not overflow.

Constraints

  • 1 <= len(X) <= 2,000
  • 1 <= len(X[0]) <= 100
  • Every row in X has the same number of features
  • Each feature value is in [-1,000, 1,000]
  • Each y[i] is either 0 or 1
  • 0 < learning_rate <= 1
  • 1 <= iterations <= 10,000
  • 0 <= l2 <= 10

Function Signature

def train_logistic_regression(X, y, learning_rate, iterations, l2):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output