Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Implement Gradient Descent in Python

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

Your question is Implement Gradient Descent in Python. 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

DeepSig needs a lightweight baseline for predicting a continuous signal-quality value from numerical radio features. Implement batch gradient descent for a linear regression model without using machine learning libraries.

Given a feature matrix X, target values y, a learning rate, and a fixed iteration count, learn one weight per feature and an intercept. Initialize all parameters to zero. On every iteration, compute predictions for the full batch, calculate the mean squared error gradients, and update all parameters simultaneously.

Formal Specification

Implement gradient_descent(X, y, learning_rate, iterations). X is a non-empty list of n rows, each containing d numeric features. y is a list of n numeric targets. Return a dictionary with keys weights and bias, where weights is a list of d floats and bias is a float.

Use these gradients:

  • dw[j] = (2 / n) * sum((prediction[i] - y[i]) * X[i][j])
  • db = (2 / n) * sum(prediction[i] - y[i])

Updates must use the parameters from the start of the iteration.

Constraints

  • 1 <= len(X) <= 10^4
  • 1 <= len(X[0]) <= 100
  • Every row in X has the same number of features
  • 0 <= iterations <= 10^4
  • 0 < learning_rate <= 1
  • X and y contain finite numeric values

Function Signature

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