The ML Lifecycle: From a Question to a Working Model
Training is only one step
When people picture machine learning, they usually picture the training part: the model learning, the error shrinking. That is the exciting bit, and it is what most tutorials jump straight to.
In a real project, training is one step out of about nine. Most of the time goes into what happens before it and after it. The full set of steps, from "we have a problem" to "a model is running and helping", is called the ML lifecycle.
The easiest way to understand it is to follow one project from start to finish. So let us do that.
The project
Meera runs a cloud kitchen in Pune that sells lunch thalis to office workers and students. Every morning she has to decide how many thalis to cook.
Cook too many and the extra food goes to waste. Right now she throws away about 40 thalis on a bad day. Cook too few and hungry customers see "sold out" and order from someone else. Today she decides based on gut feeling. She wants something better.
- 1Define the problemthis chapter
- 2Collect dataChapter 3
- 3Clean and prepareChapter 3
- 4ExploreChapters 2, 3
- 5Split off a test setChapter 3
- 6Train a modelChapters 4, 6, 7
- 7EvaluateChapters 4, 5, 6
- 8DeployChapter 8
- 9Monitor and retrainChapter 8
↻ a bad result at any step can send you back to an earlier one
Step 1: Define the problem
"Use AI to fix my kitchen" is not a problem a model can solve. Step one is turning a vague wish into an exact question. For Meera, that question is: how many lunch orders will we get tomorrow?
Making it exact tells you a lot straight away. The answer is a number, so this is regression. Past order counts exist in her records, so this is supervised learning. Both of those come from lesson 1.3.
You also decide what "good enough" means before you start. Meera says: if the prediction is usually within 15 thalis of the real number, that is a big improvement. Deciding this early stops you from moving the goalposts later.
Tip:This is also the moment to ask whether ML is needed at all, as in lesson 1.4. If orders were exactly the same every day, a simple rule would do. They are not, which is why ML is worth trying here.
Step 2: Collect data
Meera has two years of daily order counts from her delivery app dashboard. That is the label, the thing we want to predict. Now we need features: information that might explain why some days are busier than others.
This is where knowing the business matters more than knowing ML. Meera knows things no algorithm would guess:
Key Points
- Day of the week. Offices are shut on Sundays, so orders drop.
- Rain. Nobody wants to go out, so orders jump.
- College exams and holidays. Students go home in vacations.
- Big cricket matches. People stay in and order.
- Festivals. Some bring huge orders, some mean the kitchen is closed.
So the data now has one row per day: the date, the day of the week, whether it rained, whether colleges were open, whether there was a match, and how many orders came in.
Step 3: Clean and prepare the data
Real data is always messy. Meera's is no different:
Key Points
- Some days are missing because the kitchen was closed for Diwali.
- One day says 4,120 orders. It was really 412. Someone typed an extra zero.
- Dates from the first year are written as 05/03/2023 and later as 2023-03-05.
- The rain column is blank for a few weeks when nobody noted it down.
All of this has to be fixed before any model sees it. Models also only understand numbers, so things like "Monday" have to be turned into numbers too.
This step is not glamorous, and it often takes more time than every other step put together. It also matters more than almost anything else. A model trained on wrong data learns wrong patterns, no matter how clever the model is. Chapter 3 covers all of this.
Step 4: Explore the data
Before building anything, you look at the data. You draw simple charts and ask simple questions. What does a normal day look like? Which days are unusual? Does rain really change anything?
Meera's charts show a few clear things. Sundays are about half of a normal weekday. Rainy days are about 30% busier. And orders dropped sharply every May and June, when colleges were on vacation.
This step often teaches you more about the problem than the model does. It also catches mistakes the cleaning step missed. The 4,120-order day would stick out like a skyscraper on a chart.
Step 5: Split off a test set
Before training, we set some data aside and promise not to touch it. For Meera, we keep the last two months hidden. The model trains only on everything before that.
Why? Remember the two students from lesson 1.2. Checking a model on the days it trained on is like checking a student on the exact paper they memorised. The hidden two months act like a fresh exam paper. They show how the model will do on days it has never seen, which is exactly what tomorrow will be.
Step 6: Train a model
Finally, the training step. But a good ML engineer does not start with a fancy model. They start with something embarrassingly simple, called a baseline.
For Meera, the baseline is: "tomorrow will be the average of the same weekday over the last four weeks." No machine learning at all. It just gives us a number to beat. If a real model cannot beat it, the model is not worth using.
Then we train an actual model, like the linear regression you saw in lesson 1.3, using the features from step 2. Later we might try stronger models too. Chapters 4, 6 and 7 cover the main ones.
Step 7: Evaluate
Now we open the two hidden months and see how each approach does on them:
Key Points
- Meera's gut feeling: off by about 35 thalis a day on average.
- The simple baseline: off by about 25.
- The trained model: off by about 12.
The model beats both, and it clears the "within 15" goal we set in step 1. Now we can say it is worth using.
If the result had been bad, we would not just give up. We would go back. Maybe we need more features (step 2). Maybe the data has errors we missed (step 3). Going backwards like this is completely normal. That is why the lifecycle is drawn as a loop, not a straight line.
Step 8: Deploy
A model sitting in a notebook on your laptop helps nobody. Deploying means putting it where it actually gets used.
For Meera, that could be very simple. Every evening at 8 pm, a small program collects tomorrow's details: the day, the weather forecast, whether there is a match. It asks the model for a prediction and sends Meera a WhatsApp message: "Cook 318 thalis tomorrow." Chapter 8 shows how this is done.
Step 9: Monitor and retrain
Most people think the project ends at deployment. It does not, because the world keeps changing.
Six months later, a big new IT office opens down the road. Orders go up by 80 a day. The model has never seen a world with that office in it, so its predictions are now too low every single day. Nothing in the code broke. The world simply moved away from the data the model learned from. This is called drift.
So someone has to keep checking the predictions against what really happened, and retrain the model on fresh data when it starts slipping. That sends you right back to step 2, and the loop goes round again.
Where people get confused
Key Points
- "ML is mostly about picking the right algorithm." Picking the model is one step out of nine. Good data and a well-defined problem usually matter much more.
- "The model did great on the training data, so it is ready." It has to do well on data it never saw. That is what the test set in step 5 is for.
- "Once a model is deployed, the job is done." The world changes and models slowly get worse. Monitoring is part of the job.
- "The steps happen once, in order." In real projects you go back and forth between them many times.
Quick check
You find a day with 4,120 orders that should have been 412, and you fix it. Which step is that?
Step 3, cleaning and preparing the data. (Exploring the data in step 4 is often how you notice problems like this in the first place.)
Your model is off by 20 thalis a day. The simple "same weekday average" baseline is off by 18. Should you use the model?
No. It is doing worse than a rule with no machine learning in it. Either improve the model or use the simple rule. This is exactly why you build a baseline first.
A model worked well for a year and has slowly started making worse predictions, though nobody changed the code. What is probably happening?
Drift. The world changed (new customers, new competition, new habits), so the data it learned from no longer matches reality. It needs to be retrained on recent data.
Recap
Key Points
- The ML lifecycle: define the problem, collect data, clean it, explore it, split off a test set, train, evaluate, deploy, monitor.
- Start by turning a vague wish into one exact question, and decide what "good enough" means.
- Cleaning data often takes the most time and matters the most.
- Always build a simple baseline first. A model is only worth using if it beats it.
- Deployment is not the end. The world drifts, and models need checking and retraining.
Next up: the last lesson of this chapter. We have used the words features, labels and model many times now. Time to pin down exactly what each one means, especially "model".
Theia
Can you explain this concept?
Practise The ML Lifecycle: From a Question to a Working Model with realistic interview questions. Choose spoken or typed answers, get feedback on your reasoning, then return to the material that needs work.
Sign in to practise this conceptThe session stays focused on this lesson. Your selected interview level sets the bar.