Ch1. Machine Learning — Supervised Learning and Linear Regression
What Is Machine Learning?
Machine learning (ML) is a technology that enables computers to learn patterns from data and make predictions or decisions on new data — without being explicitly programmed.
Traditional Programming:
Input + Rules → Output
Machine Learning:
Input + Output → Rules (learned automatically)
This paradigm shift is the essence of ML. Instead of a developer coding every rule, an algorithm discovers the rules from data.
When ML is the right tool:
- The rules are too complex or hard to express explicitly (image recognition, natural language understanding)
- The environment changes frequently, making manual rule updates impractical (price prediction, recommender systems)
- You need to discover hidden patterns in large datasets (anomaly detection, customer segmentation)
The AI / ML / DL Hierarchy
Artificial intelligence, machine learning, and deep learning are often used interchangeably, but they have a clear containment relationship.
┌──────────────────────────────────────────────┐
│ AI (Artificial Intelligence) │
│ → Any technology that mimics human cognition │
│ ┌────────────────────────────────────────┐ │
│ │ ML (Machine Learning) │ │
│ │ → The subset of AI that learns │ │
│ │ from data │ │
│ │ ┌──────────────────────────────────┐ │ │
│ │ │ DL (Deep Learning) │ │ │
│ │ │ → The subset of ML that uses │ │ │
│ │ │ multi-layer neural networks │ │ │
│ │ └──────────────────────────────────┘ │ │
│ └────────────────────────────────────────┘ │
└──────────────────────────────────────────────┘
AI: includes game AI and rule-based expert systems
ML: statistical / optimization-based learning algorithms (linear regression, random forests, etc.)
DL: multi-layer networks with millions of parameters (CNN, RNN, Transformer)
Types of Machine Learning
Supervised Learning
Trains on data that has labeled answers. The goal is to learn a mapping function from inputs to outputs.
Training data format:
{(x₁, y₁), (x₂, y₂), ..., (xₙ, yₙ)}
x: input features
y: target label (the answer)
| Type | Output | Examples |
|---|---|---|
| Classification | Discrete class | Email spam / not spam; tumor malignant / benign |
| Regression | Continuous value | House price; tomorrow’s temperature |
Unsupervised Learning
Discovers hidden structure in data without labeled answers.
Training data format:
{x₁, x₂, ..., xₙ} ← no y (no labels)
- Clustering: group similar data points (K-means, DBSCAN)
- Dimensionality Reduction: compress high-dimensional data (PCA, t-SNE)
- Anomaly Detection: identify data that deviates from normal patterns
Reinforcement Learning
An agent learns a policy that maximizes cumulative reward through interaction with an environment.
Agent → Action → Environment
Environment → State + Reward → Agent
Examples: AlphaGo, game-playing AI, robot control
Linear Regression
Linear regression is the most fundamental regression algorithm in supervised learning. It models a linear relationship between input features and a continuous target variable.
Hypothesis Function
Simple linear regression:
h(x) = θ₀ + θ₁x
where:
h(x): predicted value
θ₀: y-intercept (bias)
θ₁: slope (weight / coefficient)
x: input feature
Intuition: θ₀ is the “baseline value” when x = 0. θ₁ is how much the prediction changes when x increases by 1.
Example: predicting house price from size (sq ft)
h(x) = 50,000 + 200x
Size x = 1,000 sq ft → predicted price = $250,000
Size x = 2,000 sq ft → predicted price = $450,000
Cost Function
The cost function quantifies how wrong the model’s predictions are. The goal of training is to find the θ₀ and θ₁ values that minimize the cost function.
MSE — Mean Squared Error
J(θ₀, θ₁) = (1 / 2m) × Σᵢ₌₁ᵐ (h(xᵢ) − yᵢ)²
where:
m: number of training samples
h(xᵢ): predicted value for sample i
yᵢ: actual value for sample i
(h(xᵢ) − yᵢ): residual error
Why square the residuals?
- Prevents positive and negative errors from canceling out
- Penalizes larger errors more heavily (squares amplify outliers)
- Makes the function differentiable — required for optimization
Intuition:
Perfect prediction → J = 0
Larger prediction errors → J increases
Training = finding θ values that minimize J
Gradient Descent
An iterative optimization algorithm that updates parameters repeatedly to minimize the cost function.
Core Intuition
Imagine a hiker descending a mountain:
- At the current position, identify the steepest downhill direction
- Take one step in that direction
- Repeat until reaching the valley (minimum)
Mountain = cost function J(θ)
Current position = current θ value
Steepest downhill = negative gradient direction (−∇J)
Step size = learning rate α
Update Rule
θ₀ := θ₀ − α × ∂J/∂θ₀
θ₁ := θ₁ − α × ∂J/∂θ₁
where:
α: learning rate (0 < α < 1)
∂J/∂θ: partial derivative (gradient) of cost function
For linear regression:
∂J/∂θ₀ = (1/m) × Σ(h(xᵢ) − yᵢ)
∂J/∂θ₁ = (1/m) × Σ(h(xᵢ) − yᵢ) × xᵢ
Learning Rate α — Why It Matters
α too large: overshoots the minimum → diverges
α too small: converges extremely slowly → computationally wasteful
α just right: cost function decreases smoothly and converges
Typical starting values: α = 0.01, 0.001
Verify by plotting cost vs. iteration
Variants of Gradient Descent
| Variant | Description | Trade-off |
|---|---|---|
| Batch GD | Updates using the full dataset | Stable; slow for large data |
| SGD | Updates on one sample at a time | Fast; noisy |
| Mini-Batch GD | Updates on small batches (32–256) | Best balance (most common) |
Multiple Linear Regression
Real data involves multiple features simultaneously affecting the target.
h(x) = θ₀ + θ₁x₁ + θ₂x₂ + ... + θₙxₙ
Vector notation:
h(x) = θᵀx
Example: predicting house price
h(x) = θ₀ + θ₁×size + θ₂×bedrooms + θ₃×age + θ₄×floor
Feature Scaling
When features have very different ranges, gradient descent converges slowly.
Example:
x₁ = house size (500 – 5,000 sq ft)
x₂ = bedrooms (1 – 6)
→ x₁ has much larger scale
→ cost function contours become elongated ellipses
→ gradient descent zig-zags inefficiently
Min-Max Normalization
x' = (x − x_min) / (x_max − x_min)
Result: all features scaled to [0, 1]
Standardization (Z-score)
x' = (x − μ) / σ
Result: mean = 0, standard deviation = 1
Advantage: less sensitive to outliers; works well for normally distributed features
For gradient-descent-based algorithms, always apply feature scaling first.
Underfitting vs. Overfitting
Underfitting (High Bias):
→ Model too simple to capture the pattern
→ Both training and test error are high
Overfitting (High Variance):
→ Model memorizes training data including noise
→ Training error low; test error high
Good Fit:
→ Balanced bias and variance
→ Generalizes well to unseen data
Train / Validation / Test Split
from sklearn.model_selection import train_test_split
# 80/20 split
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
Key Concept Cards
Hypothesis h(x) = θ₀ + θ₁x ★★★★★
The core of linear regression. θ₀ is the bias (intercept), θ₁ is the weight (slope). Training = finding the optimal θ₀ and θ₁.
Cost Function MSE and the training goal ★★★★★
J(θ) = (1/2m) × Σ(h(xᵢ) − yᵢ)². Average squared residuals. Minimizing J is the goal of training.
Gradient Descent update rule ★★★★★
θ := θ − α×(∂J/∂θ). Move in the direction opposite the gradient by step size α. Repeat until convergence.
AI ⊃ ML ⊃ DL ★★★★☆
Deep learning is a subset of ML; ML is a subset of AI. They are not synonyms.
Practice Quiz
Q1. Given h(x) = 2 + 3x, what is the prediction at x = 5? What do θ₀ and θ₁ represent?
Prediction = 2 + 3×5 = 17. θ₀ = 2 is the y-intercept (baseline prediction when x = 0). θ₁ = 3 is the slope (each unit increase in x raises the prediction by 3).
Q2. What goes wrong if the learning rate α is too large or too small?
If α is too large, gradient descent overshoots the minimum and the cost function diverges (fails to converge). If α is too small, updates are tiny, requiring a very large number of iterations — training becomes impractically slow. Try values like 0.001, 0.01, 0.1 and monitor the cost-vs-iteration curve.
Q3. What is the key difference between supervised and unsupervised learning?
Supervised learning trains on data with labeled outputs (y) — it learns an input-to-output mapping. Unsupervised learning has no labels — it discovers inherent structures (clusters, patterns) within the data itself.
OIYO Editorial
Editorial DeskThe OIYO editorial desk researches money, law, lifestyle, and self-understanding topics against primary sources and public statistics. Every piece carries source notes and is reviewed on a regular cycle for accuracy and usefulness.