What Is Machine Learning?
Rules vs learning, the vocabulary of ML, and the golden rule — never evaluate a model on the data it trained on.
Before touching any algorithm, you need three things: a clear picture of what "learning from data" actually means, the vocabulary to talk about it, and one non-negotiable habit — always keeping some data hidden from your model so you can measure how well it really performs. This lesson builds all three.
From writing rules to learning them
Suppose you're asked to build a spam filter. The classical approach is to write rules by hand: if the subject contains "FREE!!!", flag it; if the sender is unknown and there are more than three links, flag it… This works until spammers change tactics, and every fix adds another brittle rule.
Machine learning flips the recipe. Instead of writing the rules, you collect examples — thousands of emails already labeled spam or not spam — and let an algorithm find the patterns itself. The program's behavior is learned from data rather than hard-coded.
That's the relationship between the two famous buzzwords: artificial intelligence is the broad goal of making machines behave intelligently (rule-based expert systems count too), while machine learning is the subfield where that behavior is learned from examples. Almost everything called "AI" today is machine learning underneath.
Three flavors of learning
- Supervised learning — every example comes with the correct answer (a label). The model learns to map inputs to answers: spam detection, house price prediction, medical diagnosis. This is most of the course.
- Unsupervised learning — no labels at all. The model looks for structure on its own: grouping similar customers, compressing features. We'll get there in the clustering and PCA module.
- Reinforcement learning — an agent learns by acting and receiving rewards, like a game-playing bot. Fascinating, but outside our scope here.
Within supervised learning there are two main task types: classification (predict a category: spam / not spam) and regression (predict a number: tomorrow's temperature).
Samples, features, labels
Data almost always arrives as a table, and ML has names for its parts:
| Term | In the table | Convention |
|---|---|---|
| Sample (observation, instance) | one row | n_samples of them |
| Feature (attribute, predictor) | one input column | matrix X, shape (n_samples, n_features) |
| Label (target) | the column to predict | vector y |
So "train a model" means: given X and y, find a function that maps a new
row of features to a good prediction of its label.
The golden rule: never grade a model on its homework
Here's a trap every beginner falls into. You fit a model, evaluate it on the same data it trained on, see 100% accuracy, and celebrate. But a model that memorizes its training data perfectly can still be useless on new data — and new data is the only thing we care about.
Think of it like studying for an exam: if the exam questions are the exact homework problems you practiced, a perfect score proves you memorized the homework, not that you understand the subject. To measure understanding, the exam must contain questions you've never seen.
In ML the fix is the train/test split: set aside a portion of the data (typically 20–30%), train only on the rest, and evaluate on the held-out part. Watch how dramatic the difference can be — this decision tree is allowed to grow as deep as it likes on noisy data:
Perfect on training data, mediocre on test data. If we had evaluated on the training set, we would have believed we had a flawless model.
A few details in that split call matter:
test_size=0.25— hold out 25% of the rows for testing.stratify=y— keep the class proportions the same in both splits, so neither set is accidentally easier.random_state=42— make the shuffle reproducible.
Overfitting vs underfitting
The gap you just saw has a name. A model overfits when it learns the training data too well — noise, quirks, and all — so its training score is high but its test score is much lower. It memorized the homework.
The opposite failure is underfitting: the model is too simple to capture the real pattern, so both scores are low. Between the two lies the sweet spot, and controlling model complexity is how you find it:
Depth 1 underfits (both scores low), unlimited depth overfits (huge gap), and a moderate depth does best on the test set. You'll see this pattern in every model family for the rest of the course.
The test set is sacred
Use the test set once, at the end, to estimate real-world performance. If you peek at it repeatedly while tweaking your model, it silently becomes part of training and its score stops being trustworthy. Later lessons introduce cross-validation for safe, repeated evaluation during development.
The basic workflow
Every supervised project in this course follows the same four beats:
- Split —
train_test_splitbefore anything else touches the data. - Fit —
model.fit(X_train, y_train)learns patterns from training data only. - Predict —
model.predict(X_test)produces answers for unseen rows. - Evaluate — compare predictions against
y_testwith a metric such as accuracy.
Everything else — preprocessing, feature engineering, hyperparameter tuning — is elaboration on this skeleton. Memorize the beats; the next lessons add the instruments.
Check your understanding
Q1.What is the key difference between a rule-based system and a machine learning system?
Q2.In a table of house sales, each row is one house and one column is the sale price you want to predict. What is the sale price column called?
Q3.A model scores 99% accuracy on the training set and 68% on the test set. What's the most likely diagnosis?
Q4.Why pass stratify=y to train_test_split for classification?
Q5.Which sequence describes the basic supervised ML workflow?
Exercise: Watch overfitting grow with noise
Using the unlimited-depth decision tree from this lesson, generate three
datasets with make_classification at noise levels flip_y = 0.0, 0.1,
and 0.3 (keep everything else the same). For each, print the train
accuracy, test accuracy, and the gap between them. How does label noise
affect how badly the tree overfits?
Next up: your first real algorithm — K-Nearest Neighbors, a classifier so intuitive you already use it in daily life.