“Do you understand why?”
I asked Ahmed that near the end of our session. He had just shown me a bar chart with eight models on it. One was not a model at all, but a baseline that predicts “good” every time. Our goal was to beat the baseline.
“Honestly, maybe you can explain it way better than me,” he said.
I will come back to that question.
Ahmed is my mentee and a third-year AI student at Durham College. In October, he and his brother came to me for advice on getting ready for an AI internship. I wrote about that session in The internship advice I gave two students studying AI. Later that month, Ahmed showed me the first version of a sleep classifier he had built, and we reviewed how he evaluated it. I wrote about that review in Evaluating a classifier when you only have 31 rows. I asked him to simplify the model to two classes, compute the metrics until he understood them, and try a few algorithms. This session was his follow-up.
The problem he brought
Ahmed's model predicts whether his sleep will be good or bad. The model uses three features: whether he worked out that day, whether he had caffeine after 3 PM, and whether he had a lot of screen time before bed. He had 31 nights of data.
The weakness of this data set is clear. Sleep quality is subjective. He did not collect the data night by night. He looked back at his days and guessed. So the labels were mostly guesswork.
That’s fine for now. At this stage, we are not concerned about clean data. He is learning the process: how to train, validate and evaluate a machine learning classification model.
He also kept the signal leakage check from our first review. All of his features happen before he sleeps. “So there’s no cheating,” he said. Keep signal leakage on your checklist for every learning task.
Ahmed came prepared with a slide deck. Here is what we went through.
1. Start with binary classification
His first version had three classes: good, okay and bad. He merged okay into bad, which gave him 20 good nights and 11 bad ones.
He made one more change on his own, and it was the most important one. He switched the positive class from good to bad. “I want to know if I’m having a bad sleep or not,” he said. Now precision, recall and F1 all measure how well the model finds bad nights. The switch was his idea, and it was the right call.
Then I asked him if he remembered why I wanted him to simplify from multi-class to binary. He thought it was because three classes were too complicated for him. There were three reasons.
The first reason is the size of the data set. With only 31 nights and three classes, one class had 20 examples, the second had eight and the third had three. With two classes, the learning algorithm gets 11 examples of the bad class instead of three or eight.
The second reason is learning. I want him to develop an intuition for the evaluation metrics: the confusion matrix, precision and recall. With three classes, those concepts are harder to grasp. I always found it easier to start with a simpler setup, compute the numbers by hand, get the intuition, and move to a more sophisticated setup later.
The third reason is the real world. Most of the classification tasks I came across are binary. If you master binary classification and how to evaluate those models, you’ll be in pretty good shape. Then you can extend the concepts to multi-class.
2. Use stratified sampling, so every split has both classes
In the first review, Ahmed used three-fold cross-validation. Now he had moved to five-fold stratified cross-validation with a fixed seed. I asked him if he understood the reason for stratified sampling.
Stratified sampling is specific to classification tasks. Your data set has labeled examples, and every label comes from a predefined set of classes. In Ahmed’s case, every night is either good or bad.
If you do random sampling, it is possible that your evaluation split only gets good nights. Then you are not really testing whether your model can identify the bad nights. Random sampling draws some fraction of the records. It does not guarantee that your training partition and your evaluation partition both have examples from every class. Each split will likely get some bad nights, but that is mostly chance. Stratified sampling keeps that balance in every split.
Stratified sampling has a second advantage. When you evaluate the trained model, you can look at its performance for each class. Is the model doing a better job predicting good nights or bad nights? That tells you where to look next: more features, better hyperparameter tuning, or another learning algorithm.
Ahmed added why he fixed the seed: with the same split on every run, when the scores change, the change comes from the model and not from the split.
3. Expect sophisticated algorithms to do nothing on a small data set
Last time, I asked Ahmed to keep the evaluation setup fixed and try a few algorithms. He compared logistic regression, decision trees and XGBoost, and played with their hyperparameters.
He explained the C value of logistic regression in his own words. The C value controls how much the model trusts the training data. A large C trusts the data a lot, so it fits all the details and the noise. A small C is more cautious. At 0.1, his model only flagged a night when it was very sure, so it was never wrong, but it missed most of the bad nights.
Then he told me what he learned about XGBoost. XGBoost uses many trees, but his data set is so small that XGBoost finds no patterns that a single decision tree would miss.
That’s fine. Algorithms such as XGBoost benefit when you have a lot of examples. Ahmed may have read that before. Now the insight is real for him, because he learned it firsthand.
4. Walk through cross-validation one round at a time
Next, I asked Ahmed to explain five-fold cross-validation to someone who is a beginner, with a concrete example. For simplicity, I asked him to use 30 nights instead of 31.
He started well. With 30 nights split into five folds, each fold holds six nights. Then he got stuck on what happens in round one. So I took him through round one.
You have 30 labeled nights. You divide them by five, so each fold has six examples. Give the folds numbers, one through five.
In round one, you set aside fold one. You are not going to use it for training. That leaves four folds of six examples each, 24 examples in total. These 24 examples become your training data. You train a model on folds two through five. Then you use the trained model to predict the six examples in fold one, and you compute the evaluation metrics: precision, recall, F1 and the others. You record those numbers.
In round two, you set aside fold two and train a new model on the other four. You repeat this five times. At the end, you have trained five models on five slightly different training sets, and each one was evaluated on a different fold.
Now you can look at the trend. Pick accuracy. Is accuracy consistent across the five models, or is it changing a lot? Is F1 changing a lot? That tells you how well the model generalizes.
This matters because you train a model offline with historical data. Once you deploy the model to production, it works with new data, and you want some confidence that its production performance will be consistent with its offline performance. Cross-validation lets you simulate that by analyzing the model’s numbers across folds.
5. Know what each metric tells you, and what it hides
Ahmed computed every metric from the confusion matrix of his logistic regression model. His model correctly flagged 5 bad nights and correctly passed 18 good ones. It raised 2 false alarms and missed 6 bad nights.
So accuracy is 5 plus 18, divided by all 31 nights, which is 0.74. Precision is 5 out of the 7 nights the model called bad, 0.71. Recall is 5 out of the 11 real bad nights, 0.45. F1 combines the two into 0.56. His numbers matched scikit-learn exactly.
Then I explained what each number tells you.
Accuracy is the overall number: how well the model identifies both the bad nights and the good nights. Its limitation is that accuracy does not tell you whether the model is doing better with one class. Is the model good at predicting good nights and worse at predicting bad nights? That is why you look at precision, recall and the confusion matrix. F1 combines precision and recall into one score. Depending on their needs, the people who use your model may lean more toward precision or toward recall.
One more point. These numbers are only available in offline evaluation. On a live production model, you don’t know the actual labels for the incoming data. You only find out later, when you have another way to confirm whether each prediction was right. Then you can evaluate the deployed model periodically and keep improving it. That is called continuous learning.
6. Beat the baseline
This brings me back to the question I opened with: why does a model have to beat a baseline that predicts good every time?
Ahmed’s chart had eight bars: two variations of the decision tree, three of XGBoost, two of logistic regression with different C values, and a baseline that predicts the most frequent class. “It’s just predicting good every time,” he said. That baseline is no model at all, and our goal is to beat it.
When I asked him why, he said a model that predicts good every time would get 50% or more. He was on the right track, and then he asked me to explain. So I gave him an example from the real world.
Let’s say you go to Costco. You pay with your credit card. In milliseconds, the transaction goes through a machine learning model, and the model decides whether to approve the transaction. There are only two possible outcomes: this is a valid transaction, or this is a fraudulent transaction. It’s a binary classification task.
Out of all the transactions over the last year, will the fraudulent ones be the majority or a small minority? “Small minority,” Ahmed said.
Let’s say 1%. Now, if you don’t train a machine learning model and you just set a rule that says every transaction is good, your model all of a sudden will have 99% accuracy on the historical data. But it is useless. You want your model to get better at identifying that small minority, the 1%, because missing those is what costs you.
So you look at recall and precision. How is the model doing at identifying the historically known fraudulent transactions? That is recall. Say your evaluation set has 100 known fraudulent transactions, and the model correctly identifies 90 of them. The model has a recall of 0.9.
Precision looks at the times the model flags a transaction as fraudulent. Some flags are right. Some are valid transactions that the model labeled as fraud. Put the number of right flags at the top of a fraction, and all the flags at the bottom. That fraction tells you how noisy the model’s fraud alerts are.
The baseline gets high accuracy on imbalanced data without learning anything. That is why a model has to beat the baseline, on the class you care about.
7. Compute the metrics by hand, on real rows
For homework, I asked Ahmed to build a story from one fold of his own data.
Take round one, which means fold one, with six nights. Make three columns. The first is the night: night one through night six. The second is the actual label, good or bad, which you know but the model does not. The third is the model’s prediction. Some predictions will agree with the actual label and some will not.
Then explain true positive, false positive, true negative and false negative with those real rows. Map those rows into a confusion matrix. The four boxes will add up to six, because fold one has six nights. Then show the calculation of the four evaluation metrics for that fold.
I also asked him to look at the code behind his slide. With five-fold cross-validation, there are five separate models and five separate sets of numbers. Is scikit-learn showing the average from all five rounds, or one particular round? Feel free to use AI to interpret the code, but understand how the numbers are calculated.
By hand does not mean you can’t type, or that you can’t use AI to generate the plots. It means you can explain every number.
What he chose to do next
Ahmed summarized his homework in his own words. He wants to review accuracy, precision, recall and F1, play with the values until he really understands what the numbers tell him, and be able to explain the metrics in a real-world scenario.
He had a busy week ahead. I told him I don’t want us to lose momentum. He will take the next step and message me when he is ready, and we’ll look at his work together.
I know these days it may sound boring to compute these numbers by hand. You can give a prompt, and AI will come up with a very nice plot in seconds. But that’s what everyone can do. Not everyone can explain how this works behind the scenes. Those who get this deeper understanding will outpace others.






