Course Overview
Foundations of Machine LearningOrdinary Least Squares & Closed-Form Normal Equation
0% Done
Written Guide24 minutes
Ordinary Least Squares & Closed-Form Normal Equation
Lesson Summary: Deriving analytical OLS solution via linear algebra, matrix inversion, and condition numbers.
Ordinary Least Squares Normal Equation Lab
Machine LearningLesson / Article LabCompute analytical weights via (X^T X)^(-1) X^T y and compare against iterative optimization.
Ordinary Least Squares Closed-Form Normal Equation
Pyodide Wasm
Interactive Challenge: Modify code inputs, click Run Code to execute live in WebAssembly.
Wasm Terminal Output
Click Run Code to execute this algorithm in the browser sandbox.
For linear models, the closed-form analytical solution that minimizes Mean Squared Error is given by the Normal Equation:
import numpy as np
# Generate synthetic linear data
np.random.seed(42)
X = 2 * np.random.rand(100, 1)
y = 4 + 3 * X + np.random.randn(100, 1)
# Add bias column (x_0 = 1)
X_b = np.c_[np.ones((100, 1)), X]
# Compute normal equation
theta_best = np.linalg.inv(X_b.T.dot(X_b)).dot(X_b.T).dot(y)
print(f"Learned Intercept: {theta_best[0][0]:.4f}")
print(f"Learned Slope: {theta_best[1][0]:.4f}") 