Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Start free
Implement Gradient Descent in Python
00:00
5 left

Implement Gradient Descent in Python

EasyPython

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):
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements are in the Question tab.

Sign up free to run or submit. Log in
Code
Sign up free to run your code. Your draft is saved.Run with a free accountLog inLn 2
Run your code to see test output here.