"""A tiny, from-scratch house-price model to connect calculus and ML. Run with: python calculus_for_ml.py Uses only Python's standard library. """ # A model is a function: it maps house features to a predicted price. # Prices are in $100,000 units; area is in 1,000-square-foot units. def predict(area_1000_sqft, bedrooms, weight_area, weight_bedrooms, bias): return weight_area * area_1000_sqft + weight_bedrooms * bedrooms + bias def loss_half_squared(prediction, actual): # One-half times squared error makes the derivative especially simple. error = prediction - actual return 0.5 * error**2 print("1) A model is a function that makes a prediction") area, bedrooms = 1.5, 3 # 1,500 sq ft and 3 bedrooms actual_price = 2.8 # $280,000, in units of $100,000 w_area, w_bedrooms, bias = 1.0, 0.2, 0.1 prediction = predict(area, bedrooms, w_area, w_bedrooms, bias) loss = loss_half_squared(prediction, actual_price) print(f" Predicted: ${prediction * 100_000:,.0f}; actual: ${actual_price * 100_000:,.0f}") print(f" Error (prediction - actual): {prediction - actual_price:.3f} price units") print(f" Loss (0.5 * error^2): {loss:.6f}\n") print("2) Numerical limits give us derivative intuition") def square(x): return x * x x0 = 3.0 true_derivative = 2 * x0 print(f" For f(x) = x^2 at x = {x0:g}, the exact derivative is {true_derivative:.1f}.") for h in (0.1, 0.01, 0.001): # Difference quotient: compare nearby function values, then shrink the step h. estimate = (square(x0 + h) - square(x0)) / h print(f" h={h:g}: nearby-slope estimate = {estimate:.4f}") print() print("3) Derivatives and partial derivatives") # For f(x) = x^2, df/dx = 2x. A derivative measures the local change # as x changes. For multiple inputs, a partial derivative changes one input # while holding the others fixed. # Our prediction is w_area*area + w_bedrooms*bedrooms + bias, so its # partial derivatives are w_area, w_bedrooms, and 1 respectively. print(f" Prediction's partial wrt area (per 1,000 sq ft): {w_area:.3f}") print(f" Prediction's partial wrt bedrooms: {w_bedrooms:.3f}") print(f" Prediction's partial wrt bias: 1.000\n") print("4) The gradient tells each parameter how to change to reduce loss") # Let e = prediction - actual. For loss = 0.5*e^2, the chain rule gives: # dLoss/dw_area = e*area; dLoss/dw_bedrooms = e*bedrooms; dLoss/dbias = e. error = prediction - actual_price grad_area = error * area grad_bedrooms = error * bedrooms grad_bias = error print(f" Gradient: (area weight={grad_area:.3f}, bedrooms weight={grad_bedrooms:.3f}, bias={grad_bias:.3f})") print("5) One gradient-descent update") learning_rate = 0.1 w_area -= learning_rate * grad_area w_bedrooms -= learning_rate * grad_bedrooms bias -= learning_rate * grad_bias updated_prediction = predict(area, bedrooms, w_area, w_bedrooms, bias) updated_loss = loss_half_squared(updated_prediction, actual_price) print(f" Updated parameters: area weight={w_area:.3f}, bedrooms weight={w_bedrooms:.3f}, bias={bias:.3f}") print(f" New prediction: ${updated_prediction * 100_000:,.0f}") print(f" New loss: {updated_loss:.6f} (was {loss:.6f})") print(" Loss decreased, so the update was in the right direction!\n")