A food delivery app in Pune shows every customer a delivery time when they place an order. Right now the app shows the same "35 minutes" for everyone, so people who live close get their food early, and people who live far get it late and leave angry reviews.
The operations team has the records of 200 past orders. They want a model that looks at a new order and says how many minutes it will take, so the app can show an honest time.
Goal: Predict delivery_minutes (a number) for a new order. Predicting a number is a regression problem, so we start with Linear Regression.
See it visually first: move the line and watch the error bowl in 3D200 past orders, one row per order.
| order_id | distance_km | prep_minutes | peak_hour | raining | delivery_minutes |
|---|---|---|---|---|---|
| 1001 | 5.8 | 13 | No | No | 31 |
| 1002 | 8.3 | 22 | Yes | Yes | 59 |
| 1003 | 5.1 | 10 | Yes | No | 36 |
| 1004 | 7 | 8 | Yes | No | 42 |
| 1005 | 11.2 | 21 | Yes | No | 57 |
| 1006 | 8.1 | 23 | Yes | No | 52 |
First 6 of 200 rows · realistic practice data
Press Run on each step in order, or run them all at once. You can change any code and run it again.
Starting Python…
Every ML project starts by looking at the data. How many rows are there, what do the columns look like, and is anything missing?
Linear Regression draws a straight line through the data. So first we check: does delivery time go up in a straight-line way when the distance goes up? A scatter plot answers this in one look.
We hide 20% of the orders from the model. The model learns from the other 80%, and later we test it on the hidden orders, just like a real exam with questions the student has not seen.
fit is where the learning happens. The model finds the best straight line: minutes = base time + (minutes per km × distance).
The two numbers it learns have a plain business meaning, which is what you explain to the manager.
Now the exam. We predict the hidden orders and compare with what really happened.
If R² looks low, that is the real lesson: distance alone does not explain delivery time well. The next step fixes it.
Distance alone misses a lot: a rainy dinner rush is slower than a sunny afternoon. Let's add the other columns. This is multiple linear regression: one line, many inputs.
A model only understands numbers, so first we turn Yes / No into 1 / 0.
Each input gets its own number (a coefficient). It says how many minutes that input adds, keeping everything else the same. This table is the part a manager cares about most.
The model is ready to use. A customer 6 km away orders during the dinner rush while it is raining, and the restaurant needs 15 minutes. Change the numbers and run again to see how the time changes.
20 questions on new data. You write the code; hints point the way without giving it.