Skip to content
wannacode

Models: learning from examples

In short: A machine learning model learns the rule from examples (features X and a target y) with fit, predicts with predict and is checked on the test set against a baseline.

Problem

“How many people do I put in the warehouse?”

The manager makes the warehouse schedule for next week: how many people take in and pack the orders every day. Until now they guess by feel. They want a number for every day. Here are the orders from the last year, by day:

Pythondata: the shop tables

There are 365 days, and on average 30.45 orders come in per day. We count all the orders, the cancelled ones too: the question is how many arrive, and a cancelled order is also received and handled. Next to every day are the day of the week (0 is Monday, 6 is Sunday) and the month. How do you get a number for Friday, 6 November, from these examples?

The idea

The question first

Before the model you ask what decision you will make with the forecast. Here it is how many people to have every day. Three things follow from it:

  • What you predict. The number of orders in the day, not the revenue: packing depends on the orders.
  • What you know in advance. The schedule is made on Sunday. Then you know the day of the week and the month of every day. How many orders will come in until noon on Wednesday you do not know.
  • How accurate it must be. Enough not to change the decision: if the forecast misses by less than one person handles, the schedule is the same.

If the schedule does not change whatever the forecast shows, you do not need a model.

The idea

A model is a function learned from examples

A model is a function: you give it the day and it returns a forecast. You do not write the rule. You give examples, days with their known number of orders, and the model finds a rule that fits them by itself. The input is called the features and is written X. What you predict is the target, y.

The day of the week is a number from 0 to 6, but it is not a quantity: Sunday (6) is not “three times Wednesday” (2). So every value becomes its own column of 0 and 1. This is one-hot encoding, and pd.get_dummies does it:

Pythondata: the shop tables

There are 19 columns: 7 for the days and 12 for the months. 1 November 2025 is a Saturday, so its row has a 1 only in weekday_5 (and in month_11, which is further to the right).

The idea

fit, predict and the test

The models in scikit-learn have the same two steps:

  • fit(X, y) learns from the examples;
  • predict(X) gives a forecast for new rows.

Whether the model is good, you check on days it has not seen. train_test_split shuffles the rows and splits them: 75% are for training, and 25% for the test. With random_state=42 the split is the same on every run. The first load of scikit-learn (together with scipy) downloads about 18 MB and takes a few seconds:

Pythondata: the shop tables

The model learns from 273 days, and 92 days are for the test. On them it misses by 4.38 orders a day on average: the mean of the differences between the fact and the forecast, without the sign. On the training days the error is 4.09. There the model always looks better, because it has seen the answers. So the number you report is from the test.

LinearRegression is a model that adds up one number for each column. How it finds them is in the next lesson. Here only fit and predict matter, and they are the same for every model.

More precisely, if you are curious

The days are ordered in time, and train_test_split shuffles them. Here this is a simplification: the model learns about September from days after the test day too. In the next lesson you will see why for a forecast it is more honest to split by time: you train on the earlier months and check on the later ones. How a model learns by hand, without a library, is in the lesson Linear regression of the AI course.

The idea

A baseline first

Is an error of 4.38 orders good? Without a comparison you do not know. You compare with a baseline: a forecast without a model, one anyone could come up with. For example “always the mean”. DummyRegressor is exactly that, with the same fit and predict:

Pythondata: the shop tables

The baseline says 30.34 for every day: the mean of the training days. It misses by 5.76 orders, and the model by 4.38. The model is 1.38 orders more accurate, with a 24% smaller error. It makes sense.

A model that does not beat the baseline is worth nothing. It is code, data and upkeep, and it gives as much as “always 30”. That is why the baseline comes first. Another good baseline is what people do now, for example “as many as on the same day last week”.

Try it

The rule “like last week”

1. The model predicts how many orders will come in on a day from the day of the week and the month. Which is y?

Right now the manager plans “as many as on the same day last week”. How much does this rule miss on the test days? Put in last_week the orders from 7 days earlier for every day, and in err the mean error without the sign on the days of y_test.

Pythondata: the shop tables

Exercise

Is the day enough? And the plan for next week

Is the day of the week enough on its own, without the month? Fit LinearRegression only on the weekday columns of X_train. Put in err the mean error on the test days, in base the error of “always the training mean”, and in beats whether the model beats it.

Pythondata: the shop tables

The test said that the model with the day and the month beats the baseline. Now the forecast for the real week, from Monday 2 to Sunday 8 November 2026. Fit the model again on all 365 days, make X_new with the columns of X and put in plan the forecast for every day (a Series with the dates as the index).

Pythondata: the shop tables

Test

Check yourself

1. The model misses by 4.09 orders on average on the training days and by 4.38 on the test days. Which number do you tell the manager?
2. A new model misses by 5.9 orders a day on average, and “always the mean” by 5.76. What do you do?
3. Why does the day of the week go into the model as 7 columns of 0 and 1, and not as one number from 0 to 6?
4. The warehouse schedule is made on Sunday for the whole next week. Can a feature be “the orders until noon on the same day”?

Made by Ivo Gergov as part of Evo Tech's campaign for accessible programming education ·