TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
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.

Return the weight vector as a list of floats with one value per feature column.

Loading visualization...

Examples

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

Output: [2.0]

Explanation: The single weight 2 exactly maps each feature value to its target.

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

Output: [1.0, 2.0]

Hint 1

Convert X and y to float64 NumPy arrays.

Hint 2

Apply matrix multiplication in the same order as the normal equation.

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.

Return the weight vector as a list of floats with one value per feature column.

Loading visualization...

Examples

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

Output: [2.0]

Explanation: The single weight 2 exactly maps each feature value to its target.

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

Output: [1.0, 2.0]

Hint 1

Convert X and y to float64 NumPy arrays.

Hint 2

Apply matrix multiplication in the same order as the normal equation.

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.