Up to now, every query we write follows a rule that we type ourselves. For example, CASE WHEN total_amount >= 15000000 THEN 'Large' is a rule that a person chose. This works when the rule is easy to state. On a table with millions of customers, however, no one can write down which of them are about to stop buying. Machine learning is the tool for that second kind of problem, so we start with what it does and what it needs.
Rules from people, rules from data
In normal programming, a person writes the rule and the computer applies it. In machine learning, a person provides examples and the computer finds the rule. To judge the rule, we later test it on rows that it has not seen.
| Rule written by a person | Rule learned from data | |
|---|---|---|
| You provide | The rule | Past rows with the answer |
| You get | Output for each row | A model that predicts new rows |
| Example | Orders above 15 million are Large | Which customers will not order again |
The words you will meet
Four words cover most conversations. A feature is one input column, such as the number of orders in the last 90 days. A label is the answer we want to predict, such as whether the customer ordered again. Training means letting the program study rows where the label is known. A prediction is the model's answer for a new row.
- The features can be
orders_90d,days_since_last_orderandavg_order_value. - The label is
reordered, which is 1 if the customer ordered again and 0 if not. - The prediction is a probability or a yes or no for each new customer.
Supervised and unsupervised learning
This is a classification problem when the label is a category, and a regression problem when the label is a number. Both belong to supervised learning, because every training row comes with an answer. Unsupervised learning has no label. It looks for structure on its own, as in grouping customers into segments.
A shop owner does the supervised version without thinking about it. After a year of watching customers, she notices who comes back. Then she guesses which new customer will return. The model does the same with more rows and less guessing.
Where data analysts use it
- Churn prediction finds the customers who may stop buying.
- Forecasting estimates next month's orders or revenue.
- Segmentation groups customers that behave alike.
- Scoring ranks the leads that are more likely to buy.
Many of these questions can also be answered with SQL alone. A GROUP BY on the last order date already gives a basic churn list. A model earns its place when the pattern depends on many columns at once, since a person cannot write that rule by hand.
What can go wrong
A model learns whatever is in the data, including the mistakes. If the history has duplicate rows or missing values, the model learns from them too. The score on training rows also looks better than it will be in real use. For this reason, we always test on rows that were held back, which we show in the post on a first model with scikit-learn.


