ML Fundamentals
Beginner 10 min read ML Fundamentals

Machine Learning Introduction

Patterns that generalize

Machine learning finds patterns in data that generalize to unseen examples: supervised (labeled) vs unsupervised (unlabeled), with success judged by generalization, not in-sample fit.

machine learningsupervisedunsupervisedgeneralizationlabelmodel
Prerequisites: EDA Introduction
Before we start

What you will learn

  • Define supervised and unsupervised learning with examples
  • Explain generalization as the real objective of ML
  • Identify regression vs classification from the target type
Introduction

What is this?

Machine learning is the automatic discovery of patterns in data that transfer to new, unseen examples. Supervised learning maps features X to a labeled target y (classification for categories, regression for continuous values); unsupervised learning finds structure (groups, densities, components) in X alone. Success is generalization — the pattern must hold beyond the training rows, or the model just memorized.

Motivation

Why do you need it?

  • Hand-written rules don't scale: fraud, vision, language and demand change faster than humans can codify.
  • ML surfaces patterns humans can't see: subtle interactions, complex non-linearities, rare-event signatures.
  • A correct generalization loop is measurable and auditable: splits, metrics, monitoring.
  • It is the engine behind most data products: recommenders, forecasts, risk and anomaly systems.
Think about it

Mental model

Imagine

Learning to drive on a test track

Think of

Training a model

Passing the exam course (training) is easy; driving safely on any road (generalization) is the actual skill. Overfitting is passing the course by a route you memorized.

Mechanics

How it works

  • Frame the question: what prediction, what target, what cost of being wrong?
  • Curate data: features X and labels y (or unlabeled for unsupervised).
  • Split into train/validation/test so generalization is measurable.
  • Train a family of models; validate hyperparameters on validation.
  • Score the final fit on the untouched test set; if it generalizes, deploy with monitoring.

The ML lifecycle.

Frame
Data
Split
Fit
Confirm
Hands-on

Examples

Supervised vs unsupervised at a glance

Supervised vs unsupervised at a glance
import numpy as np
from sklearn.linear_model import LogisticRegression, LinearRegression
from sklearn.cluster import KMeans
# supervised: classification (churn) and regression (LTV)
X = np.random.rand(200, 3)
y_cls = (X[:, 0] + 0.6 * X[:, 1] > 0.8).astype(int)
y_reg = 5 * X[:, 1] + np.random.normal(0, 0.1, 200)
cls = LogisticRegression().fit(X, y_cls)
reg = LinearRegression().fit(X, y_reg)
print("classification acc:", round(cls.score(X, y_cls), 3))
print("regression r2:", round(reg.score(X, y_reg), 3))
# unsupervised: clustering finds structure without labels
k = KMeans(n_clusters=3, n_init=10, random_state=0).fit(X)
print("cluster sizes:", np.bincount(k.labels_))

Classification and regression use labeled targets; clustering organizes rows without labels — the dividing line between supervised and unsupervised learning.

Real world

In production

  • Fraud detection: supervised classifier on labeled transactions; unsupervised anomaly detection flags novel attack modes.
  • Recommendation: supervised CTR models + unsupervised embeddings for cold-start.
  • Demand forecasting: supervised regression over lagged, engineered features.
Guidelines

Best practices

  • Start from a clearly-scored baseline (heuristic or trivial model); record the exact data split and preprocessing at every run; treat the business cost of errors as part of the framing, not an afterthought.
Watch out

Common mistakes

  • Judging a model by train accuracy; leaking test data into feature engineering; forgetting to define the baseline before fancy models; choosing classification over regression because 'it's the default'.
When things break

Debugging tips

  • Before any model, tabulate your label distribution — a 99.9% majority class makes raw accuracy meaningless.
  • Check feature count vs row count early; thousands of features with hundreds of rows needs regularization.
  • Reproduce a trivial baseline (predict the mean/majority) to know what 'good' means.
Interviews

Interview checkpoint

Test yourself

Quick quiz

0/2
1

Which is unsupervised learning?

2

A model nails training accuracy but flops on new data. The core issue is:

Go deeper

Official documentation