#!/usr/bin/env python3
# Source: t5-linear-algebra.html
import numpy as np

rng = np.random.default_rng(0)
X = rng.normal(size=(100, 3))
w_true = np.array([1.0, -2.0, 0.5])
y = X @ w_true + 0.1 * rng.normal(size=100)

w, *_ = np.linalg.lstsq(X, y, rcond=None)   # SVD-based solver
r = y - X @ w                                # residual
print(np.round(w, 3))                        # close to w_true
print(np.abs(X.T @ r).max() < 1e-9)          # residual is orthogonal to col(X)
# [ 0.999 -2.013  0.484]
# True
