🎉 75% of content is free forever — Unlock Premium from $10/mo →
CW
💼 Servicesℹ️ About✉️ ContactView Pricing Plansfrom $10

Linear Regression: Math, Code and Assumptions

Module 7: Machine Learning FundamentalsLinear Regression🟢 Free Lesson

Advertisement

Linear Regression: Math, Code and Assumptions

The Foundation of Machine Learning

Linear regression is the most fundamental algorithm in ML. Despite its simplicity, understanding it deeply provides insight into all supervised learning methods.

ML Algorithm Landscape

Supervised Learning AlgorithmsLinearLinearRegressionLogisticRegressionRidge/LassoTree-BasedDecisionTreeRandomForestXGBoostNeuralPerceptronMLPDeepLearningSupportLinearSVMKernelSVMSVR

Linear Regression is the foundation — understand this first!


1. Simple Linear Regression

Mathematical Formulation

Model:

Where:

  • = intercept (bias) — value of when
  • = slope (weight) — change in for unit change in
  • = error term —
Simple Linear Regression: Finding the Best Fit LineFeature (x)Target (y)eᵢeᵢeᵢeᵢeᵢeᵢβ₀ = interceptβ₁ = slopeActual data pointsRegression lineResiduals (errors)

How this diagram works: This diagram shows the core concept of simple linear regression — fitting a straight line through scattered data points to model the relationship between a feature (x) and a target (y). The blue data points represent actual observations, while the purple regression line represents the model's predictions. The red dashed lines (residuals) show the vertical distance between each data point and the line, representing prediction errors. The goal of linear regression is to minimize these residuals by finding the optimal intercept (β₀) and slope (β₁) that produce the smallest total squared error.


2. Cost Function (Ordinary Least Squares)

Mean Squared Error (MSE):

Goal: Find that minimize

Closed-Form Solution (Normal Equation):

Cost Function: The Bowl-Shaped SurfaceGlobal MinimumGradient DescentGradient Descentβ₁ (slope)J(β₀, β₁)

The cost function is convex — gradient descent finds the global minimum


3. Gradient Descent

Update Rule:

Partial Derivatives:

Where = learning rate (step size)

Gradient Descent: Learning Rate Impactα = 0.1 ✓α = 1.0 ≤ (oscillates)α = 0.001 (too slow)Good learning rateToo largeToo small

4. Multiple Linear Regression

Model:

Matrix Form:

Where (design matrix with intercept column)

Normal Equation (Matrix):

Multiple Regression: Multiple Features → Single Outputx₁(Size)βx₂(Beds)βx₃(Age)βx₃(Baths)βLinearModelŷ = β₀ + Σβ⊥x⊥Outputŷ (Price)

5. Model Evaluation Metrics

R² Score (Coefficient of Determination):

  • : Perfect fit
  • : Model predicts the mean
  • : Model is worse than predicting the mean

Adjusted R²:

R² Score: How Well Does the Model Fit?SS_total = Σ(yᵢ - ȳ)² = Total VarianceSS_explained = Σ(ŷᵢ - ȳ)² = 70%SS_residual = 30%

R² = 1 - (30/100) = 0.70 (70% variance explained)


6. Assumptions of Linear Regression

5 Key Assumptions to Validate1. Linearityy = f(x) is linear2. IndependenceErrors are independent3. HomoscedasticityConstant variance4. Normality of Errorsε ~ N(0, σ²)5. No MulticollinearityX₁ → X₂Features not correlated

Checking Assumptions with Residual Plots

Residual Analysis: What to Look For✓ Good: Random≤ Bad: Funnel≤ Bad: Pattern

7. Implementation in Python


Key Takeaways

  1. Linear regression finds the best-fit line through data points
  2. Cost function (MSE) measures prediction error — minimize it
  3. Gradient descent iteratively updates weights to find minimum
  4. R² score tells you how much variance the model explains
  5. Validate assumptions before trusting the model
  6. Regularization (Ridge/Lasso) prevents overfitting

Next: Logistic Regression

Extend linear regression to classification with the sigmoid function.

Need Expert Data Science Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement