TensorTonicTensorTonic
Problems
Study PlansProjectsInterviewPricingFeedback
Problems
Loading...
1 / 1

Linear Regression Closed Form

Linear AlgebraClassic ML
Medium

Linear regression finds the weight vector that minimizes the sum of squared errors between predictions and targets. The closed-form solution (the normal equation) computes the optimal weights directly using matrix algebra, without iterative optimization.

Given a feature matrix XXX and a target vector yyy, compute the weight vector www using the normal equation.

Formula

w=(XTX)−1XTyw = (X^T X)^{-1} X^T yw=(XTX)−1XTy

Where XXX is the n×dn \times dn×d feature matrix (nnn samples, ddd features), yyy is the nnn-dimensional target vector, and www is the ddd-dimensional weight vector.

Loading visualization...

Examples

Input: X=[[1],[2],[3]]X = [[1], [2], [3]]X=[[1],[2],[3]], y=[2,4,6]y = [2, 4, 6]y=[2,4,6]

Output: [2.0][2.0][2.0]

The line y=2xy = 2xy=2x perfectly fits the data.

Input: X=[[1,1],[1,2],[1,3]]X = [[1, 1], [1, 2], [1, 3]]X=[[1,1],[1,2],[1,3]], y=[3,5,7]y = [3, 5, 7]y=[3,5,7]

Output: [1.0,2.0][1.0, 2.0][1.0,2.0]

The first column acts as a bias. The model y=1+2xy = 1 + 2xy=1+2x fits perfectly.

Hint 1

Look into np.linalg.inv\texttt{np.linalg.inv}np.linalg.inv for computing (XTX)−1(X^T X)^{-1}(XTX)−1.

Hint 2

NumPy's @\texttt{@}@ operator chains matrix multiplications, so the entire formula can fit in one expression.

Requirements

  • Use numpy to convert XXX and yyy to arrays
  • Compute the transpose XTX^TXT
  • Compute the products XTXX^T XXTX and XTyX^T yXTy
  • Compute the inverse (XTX)−1(X^T X)^{-1}(XTX)−1
  • Multiply the inverse by XTyX^T yXTy to get www
  • Return a list of floats or a numpy array

Constraints

  • XXX has at least as many rows as columns (n≥d)(n \geq d)(n≥d)
  • XTXX^T XXTX is always invertible
  • Return length must equal ddd, the number of columns in XXX
  • numpy is available as np\texttt{np}np
  • Time limit: 300 ms
Try Similar Problems
Ridge RegressionGaussian Naive BayesLogistic Regression TrainingPearson CorrelationMatrix Inverse

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: array

You must run your code first.
PrevNext

Linear Regression Closed Form

Linear AlgebraClassic ML
Medium

Linear regression finds the weight vector that minimizes the sum of squared errors between predictions and targets. The closed-form solution (the normal equation) computes the optimal weights directly using matrix algebra, without iterative optimization.

Given a feature matrix XXX and a target vector yyy, compute the weight vector www using the normal equation.

Formula

w=(XTX)−1XTyw = (X^T X)^{-1} X^T yw=(XTX)−1XTy

Where XXX is the n×dn \times dn×d feature matrix (nnn samples, ddd features), yyy is the nnn-dimensional target vector, and www is the ddd-dimensional weight vector.

Loading visualization...

Examples

Input: X=[[1],[2],[3]]X = [[1], [2], [3]]X=[[1],[2],[3]], y=[2,4,6]y = [2, 4, 6]y=[2,4,6]

Output: [2.0][2.0][2.0]

The line y=2xy = 2xy=2x perfectly fits the data.

Input: X=[[1,1],[1,2],[1,3]]X = [[1, 1], [1, 2], [1, 3]]X=[[1,1],[1,2],[1,3]], y=[3,5,7]y = [3, 5, 7]y=[3,5,7]

Output: [1.0,2.0][1.0, 2.0][1.0,2.0]

The first column acts as a bias. The model y=1+2xy = 1 + 2xy=1+2x fits perfectly.

Hint 1

Look into np.linalg.inv\texttt{np.linalg.inv}np.linalg.inv for computing (XTX)−1(X^T X)^{-1}(XTX)−1.

Hint 2

NumPy's @\texttt{@}@ operator chains matrix multiplications, so the entire formula can fit in one expression.

Requirements

  • Use numpy to convert XXX and yyy to arrays
  • Compute the transpose XTX^TXT
  • Compute the products XTXX^T XXTX and XTyX^T yXTy
  • Compute the inverse (XTX)−1(X^T X)^{-1}(XTX)−1
  • Multiply the inverse by XTyX^T yXTy to get www
  • Return a list of floats or a numpy array

Constraints

  • XXX has at least as many rows as columns (n≥d)(n \geq d)(n≥d)
  • XTXX^T XXTX is always invertible
  • Return length must equal ddd, the number of columns in XXX
  • numpy is available as np\texttt{np}np
  • Time limit: 300 ms
Try Similar Problems
Ridge RegressionGaussian Naive BayesLogistic Regression TrainingPearson CorrelationMatrix Inverse

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: array

You must run your code first.