Part V: First Models
Teaching Machines to Make Predictions (and Learning What That Really Means)
This is the part you have been waiting for. If you have heard about machine learning, if you have read breathless headlines about AI predicting everything from stock prices to disease outbreaks, if you have wondered what is behind those predictions and whether you could build one yourself — this is where that happens.
Over the next six chapters, you will build three predictive models, evaluate them rigorously, and assemble them into a professional machine learning workflow. You will predict vaccination rates from country-level indicators. You will classify countries as high or low vaccination coverage. You will teach a decision tree to learn complex patterns and then combine hundreds of trees into a random forest that performs better than any single tree could.
It is going to be exciting. But before we begin, let me be honest with you about something the headlines often get wrong.
The Part They Do Not Tell You
Machine learning is powerful. It is also, in its introductory form, much simpler than most people expect. Linear regression — your first model — is essentially drawing a best-fit line through a cloud of points. You may have done this in a high school science class, by hand, with a ruler. The machine learning version does the same thing, just with more dimensions and a precise mathematical definition of "best." Logistic regression is the same idea, bent into an S-curve for yes-or-no questions. Decision trees ask a series of yes-or-no questions about your data features, like a flowchart or a game of twenty questions.
None of these models are magic. They are tools — powerful, useful, sometimes transformative tools, but tools with limitations, assumptions, and failure modes. Part V will teach you to use them, and just as importantly, to understand when they work, when they fail, and why the difference matters.
The other thing the headlines get wrong is that the algorithm is the hard part. It is not. The hard part is everything you have already done: asking a good question, getting data, cleaning it, exploring it, visualizing it, thinking statistically about what it means. A perfectly tuned random forest trained on bad data will produce confident, precise, and completely wrong predictions. The work you did in Parts I through IV is not a prelude to the real work — it is the foundation that makes this work possible.
What You Will Find in These Chapters
Chapter 25: What Is a Model? starts with fundamentals before touching any algorithm. What does it mean to model something? A model is a simplified representation of reality — a map, not the territory. This chapter introduces the two goals of modeling (prediction versus explanation), the central tension of modeling (the bias-variance tradeoff), and the essential discipline of modeling (separating training data from test data). You will learn about overfitting — the trap of building a model so complex that it memorizes the training data rather than learning the underlying pattern — through visual examples of fitting curves to scatter plots. A perfectly curvy line that passes through every training point looks great until you show it new data and it fails spectacularly. This chapter also introduces scikit-learn, the Python library that will be your modeling companion, and walks you through your first train_test_split.
Chapter 26: Linear Regression is your first real model, and there is a reason it comes first. Linear regression is interpretable in a way that more complex models are not. When you fit a model predicting vaccination rates from GDP, the coefficient tells you directly: "For each $1,000 increase in GDP per capita, the model predicts vaccination rate increases by X percentage points." You can explain that to a policymaker. You can argue about whether it is causal. You can check the assumptions and see where the model struggles. You will build models with one predictor and then multiple predictors, interpret the output using both scikit-learn and statsmodels, evaluate fit using R-squared and residual plots, and check whether the model's assumptions hold for your data.
Chapter 27: Logistic Regression and Classification shifts the question from "how much?" to "which category?" Not all predictions are numbers. Sometimes you need to predict whether a country will achieve high or low vaccination coverage, whether a customer will churn, whether an email is spam. Logistic regression handles this by predicting probabilities — a number between 0 and 1 that you then convert to a category using a threshold you choose. That threshold choice matters enormously: setting it high means you rarely predict "high vaccination" unless you are very confident, which means fewer false alarms but more missed cases. Setting it low means you catch more cases but also more false positives. This tradeoff — between precision and recall, between false positives and false negatives — is at the heart of classification, and it depends entirely on what the predictions will be used for.
Chapter 28: Decision Trees and Random Forests introduces models with a superpower: you can show them to your boss. A decision tree makes predictions by asking a sequence of questions — "Is GDP above $10,000? Is healthcare spending above 5% of GDP? Is the country in Sub-Saharan Africa?" — and following the answers to a prediction. You can visualize the tree and trace its logic, which makes it enormously valuable when you need to explain how a prediction was made. Random forests take this idea further by training hundreds of trees on random subsets of the data and averaging their predictions. The result is almost always more accurate than any individual tree, at the cost of some interpretability. You will build both, visualize them, extract feature importance rankings, and compare them to your regression models.
Chapter 29: Evaluating Models is the reality check. Building a model is easy. Knowing whether it is any good is hard. Accuracy — the percentage of correct predictions — is the most intuitive metric and also the most misleading. If 90% of countries in your dataset have high vaccination rates, a model that always predicts "high" gets 90% accuracy while being completely useless. This chapter teaches you the metrics that matter: the confusion matrix, precision, recall, F1 score, ROC curves, and AUC. More importantly, it teaches you that the right metric depends on the cost of different types of errors. A model that misses countries with dangerously low vaccination rates is far worse than one that occasionally flags a country unnecessarily. You will evaluate all three of your models side by side, using consistent methodology and appropriate metrics.
Chapter 30: The Machine Learning Workflow brings everything together into a disciplined, repeatable process. In practice, data scientists do not build models by hand-coding each step. They build pipelines — automated sequences that chain preprocessing (scaling, encoding, imputation) with model training, cross-validation, and hyperparameter tuning. You will learn scikit-learn Pipelines, k-fold cross-validation, GridSearchCV for hyperparameter tuning, and the absolutely critical concept of data leakage — the subtle error of allowing information from the test set to influence preprocessing or training, which produces performance estimates that are optimistic lies. This chapter is the difference between someone who has trained a model and someone who can build a trustworthy modeling workflow.
The Progressive Project Reaches Its Climax
Your vaccination project has been building toward this moment. You defined questions in Part I, wrangled data in Part II, visualized patterns in Part III, and established statistical rigor in Part IV. Now you turn those patterns into predictions. Can country-level indicators predict vaccination rates? You will frame the problem, select features, build three different models, evaluate them honestly, and assemble a complete pipeline. The model comparison table you produce — linear regression versus logistic regression versus random forest, evaluated with proper train-test splits and multiple metrics — is a centerpiece deliverable of the entire book.
Managing Expectations
Let me set a realistic expectation: your models will not be perfect. They might not even be particularly good by the standards of production machine learning. The vaccination dataset is small, the features are limited, and the relationships are complex. That is fine. That is, in fact, the point.
The goal of Part V is not to build a model that changes the world. The goal is to understand what models do, how they work, when they fail, and how to evaluate them honestly. These skills transfer to any modeling problem you will ever encounter, whether you are predicting vaccination rates or house prices or customer behavior or disease diagnoses.
The students who get the most out of Part V are not the ones who achieve the highest accuracy scores. They are the ones who can explain why their model makes the predictions it does, what assumptions it relies on, where it is likely to fail, and what data would make it better. That is the difference between running a machine learning algorithm and doing data science.
What Comes After
Part VI takes you from someone who can build a model to someone who can be a data scientist. You will learn to communicate your findings, consider the ethical implications of your work, make your analysis reproducible, build a portfolio, complete a capstone project, and plan your career. The models you build in Part V are not the end of the journey — they are the evidence you will present, the tools you will explain, and the artifacts you will polish into a professional body of work.
But that is later. Right now, it is time to build your first model.
Let us teach a machine to learn.
Chapters in This Part
- Chapter 25: What Is a Model? Prediction, Explanation, and the Bias-Variance Tradeoff
- Chapter 26: Linear Regression — Your First Predictive Model
- Chapter 27: Logistic Regression and Classification — Predicting Categories
- Chapter 28: Decision Trees and Random Forests — Models You Can Explain to Your Boss
- Chapter 29: Evaluating Models — Accuracy, Precision, Recall, and Why "Good" Depends on the Question
- Chapter 30: The Machine Learning Workflow — Pipelines, Validation, and Putting It All Together