✎ Edit content·DAY 028 · POST 4 OF 5 · Code Example

Supervised Learning

Machine Learning · 12 slides
DAY 028 · POST 4 OF 5
(REMINDER)
DAY 028
Train a Model You Can Run
@saurav_dnj_24github.com/SauravDnj · linkedin.com/in/sauravdnj
1 / 12

Theme

Palette

Download

4K — sharpest, slowest
🎬 Video options
Preparing preview…
Live preview · loops the “none” effect
All rendering runs in your browser. No server, no cost, no upload. MP4/WebM = full motion + effects · GIF = lightweight loop · PNG/PDF = static for the Instagram & LinkedIn carousel.

Caption (tap to copy)

📖 Deep dive (full written explanation)

The slides stay clean and scannable. Here's the in-depth explanation behind each one — great for the blog version, show notes, or studying the topic properly.
Slide 1 · Train a Model You Can Run

This is the hands-on post, where supervised learning stops being diagrams and becomes code you can paste and run. Every block uses scikit-learn and a built-in dataset, so there is nothing to download and nothing to configure — the snippets run as-is in any notebook with scikit-learn installed.

The deliberate ordering mirrors the pipeline from the previous post: import, load labeled data, split, fit, predict, evaluate. By the end you have a trained classifier, an honest accuracy number, and a confusion matrix showing exactly which classes it confuses. That full round trip is the goal.

Slide 2 · 0. Install + import

Step zero gathers the imports. We pull in load_iris for a clean labeled dataset, train_test_split to carve out an honest test set, RandomForestClassifier as a strong general-purpose model, and two metrics — accuracy_score and confusion_matrix — for evaluation. Each import names exactly the tool used in a later step.

A random forest is a good default first model: it handles numeric features without scaling, resists overfitting reasonably well thanks to averaging many trees, and needs almost no tuning to get a solid baseline. It lets us focus on the workflow rather than on hyperparameters.

Slide 3 · 1. Load labeled data

Step one loads the Iris dataset, the classic introductory classification problem. X holds 150 rows of four flower measurements each — sepal and petal length and width — and y holds the matching species label as 0, 1, or 2. Printing the shapes and class set confirms you have features and labels lined up.

Iris is ideal for teaching because it's small, clean, and almost perfectly separable, so the mechanics stay front and center without data-cleaning distractions. The same code works on harder built-in datasets like load_wine or load_digits when you want a tougher test, which the tips slide suggests.

Slide 4 · 2. Split train / test

Step two performs the most important discipline in the workflow: the train/test split. We hold out 20% of the data as a test set the model will never train on. random_state fixes the shuffle so the split is reproducible across runs, and stratify=y preserves the proportion of each species in both halves.

Stratification matters more than beginners expect. Without it, a random split can over- or under-represent a class by chance, skewing both training and the test estimate. For classification, stratifying on the label is a cheap, almost-always-correct default that keeps your evaluation fair.

Slide 5 · 3. Fit the classifier

Step three creates the model and fits it. We ask for 100 trees and fix the random seed for reproducibility, then call fit on the training data alone. That single fit call is the entire learning step — everything the how-it-works post described as a training loop happens inside it.

Notice that fit touches only X_train and y_train. The test set is deliberately absent here; introducing it now would corrupt the honesty of the later evaluation. Keeping fit strictly on training data is the practical embodiment of the train/test discipline.

Slide 6 · What fit() just did

This explanatory slide unpacks what the one-line fit actually did so the magic doesn't feel like a black box. Internally the forest grew 100 decision trees, each trained on a bootstrap sample of the rows and learning threshold rules on the four measurements that separate the three species.

The key reassurance is that nothing touched the test set, and all the learned knowledge now lives in the model's internal structure. When we call predict next, the forest will route each new flower down its trees and take a majority vote — no further learning, just applying what fit captured.

Slide 7 · 4. Predict on unseen data

Step four runs inference: we call predict on the held-out test features to get the model's guessed species for flowers it never saw during training. Printing the first few predictions next to the true labels gives an immediate, concrete sense of how the model is doing before we compute any aggregate metric.

This is the payoff phase the definition post described — applying the trained mapping to new, unlabeled-in-spirit data. In a real deployment, y_test wouldn't exist yet; here we kept it aside precisely so we can grade the predictions against ground truth in the next step.

Slide 8 · 5. Score it honestly

Step five computes accuracy: the fraction of test predictions that match the true labels. On Iris a random forest typically lands around 96–97%, formatted here as a percentage. Crucially, this number comes from the test set, so it estimates how the model behaves on genuinely new data, not how well it memorized.

The inline comment underscores the rule that gives accuracy its meaning: it must be measured on data the model has never seen. The same metric computed on the training set would be near-perfect and worthless as a guide to real performance — a trap the mistakes post calls out directly.

Slide 9 · 6. Where it slips

Step six reads the confusion matrix, which is far more informative than a single accuracy number. Rows are the true classes and columns are the predicted ones, so the diagonal counts correct predictions and every off-diagonal entry is a specific mistake. The annotated example shows one class-1 flower misclassified as class-2.

This matrix tells you not just how often the model is wrong but how it's wrong — which classes it confuses with which. On imbalanced or high-stakes problems, that structure matters enormously, because some confusions are far costlier than others. It's the natural bridge to precision, recall, and the metrics the final post recommends.

Slide 10 · What the build does

The pipeline diagram recaps the whole build as four stages — load, split, fit, evaluate — mirroring the code blocks you just ran. It's a reminder that despite the six snippets, the conceptual workflow is short and always the same regardless of dataset or model.

Internalizing this four-stage shape means you can sit down with any new supervised problem and immediately scaffold it: get labeled data in, hold out a test set, fit a model, and measure honestly on the holdout. The specific algorithm becomes a swappable detail inside an unchanging frame.

Slide 11 · Make it your own

The tips slide turns the demo into a launchpad for your own experiments. Swapping RandomForestClassifier for LogisticRegression shows how little else changes when you switch models — the fit/predict/score interface is identical across scikit-learn estimators, which is one of the library's great strengths.

The other suggestions deepen understanding: harder datasets stress the model, classification_report adds precision and recall, varying test_size shows how the estimate's stability depends on holdout size, and feature_importances_ reveals which measurements the forest actually relied on. Each is a small, safe edit that teaches something real.

Slide 12 · Save this. Follow for Day 29.

The cover frames this as the practical, runnable post in the arc — the moment theory turns into a working classifier. It pairs naturally with the how-it-works post, putting concrete scikit-learn calls behind each abstract stage of the training pipeline.

The teaser sets up the final post on common mistakes. Now that you can train and score a model, the next essential skill is recognizing the ways that score can lie to you — leakage, bad metrics, overfitting — before any of them reach production.

🎨 AI image prompt (matches this theme + palette)

Paste into Midjourney, DALL·E, Ideogram, etc. to generate an on-brand image, then upload it on the Edit content page. The prompt updates automatically with the selected theme + palette.