Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Linear Regression in NumPy

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

Your question is Linear Regression in NumPy. 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

Write a code for linear regression in bare NumPy.

Implement ordinary least squares using only NumPy, automatically adding an intercept term and supporting multiple features. The implementation should remain defined for rank-deficient or underdetermined design matrices by returning the minimum-norm least-squares solution. Input: X, a numeric 2D array of shape (n_samples, n_features), and y, a numeric 1D array of shape (n_samples,). Output: a 1D array of shape (n_features + 1,), ordered as intercept followed by feature coefficients.

Constraints

  • 1 <= n_samples <= 5000
  • 1 <= n_features <= 100
  • X and y contain finite numeric values
  • len(y) == number of rows in X
  • The output includes an intercept before all feature coefficients

Function Signature

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