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.
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
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.
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.
Mental model
Learning to drive on a test track
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.
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.
Examples
Supervised vs unsupervised at a glance
import numpy as npfrom sklearn.linear_model import LogisticRegression, LinearRegressionfrom 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 labelsk = 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.
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.
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.
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'.
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.
Interview checkpoint
Quick quiz
Which is unsupervised learning?
A model nails training accuracy but flops on new data. The core issue is: