Supervised Learning: Features, Labels, Classification and Regression
What supervised learning is, shown on one small job: sorting support tickets into billing and technical. Rules vs learning from examples, classification vs regression, why some tickets stay hidden, and a 12-ticket classifier that labels three new ones.
What you will learn
- Explain supervised learning with features, labels, a model and a prediction
- Tell a classification problem from a regression problem
- Say why a hidden test set matters
- Train a tiny ticket classifier with fit, then read predict and predict_proba
Before you start
- Basic Python
Imagine you run a support inbox. Every day new tickets arrive, and each one has to reach the right team. There are two queues: billing, for charges, invoices and refunds, and technical, for crashes, logins and errors.
Here's the useful part. The old tickets were already sorted by hand, so each one carries its right answer. Can a program learn from those old tickets? That question is what supervised learning is about, and we'll answer it with a twelve-ticket example from start to finish.
Write rules, or learn from examples
Think about how a small child learns fruit names. You show a card with an apple and say "apple". A banana, "banana". Another apple. Many times. Then you show a new picture, and the child names it.
There are two ways to sort tickets.
- Option 1: write rules by hand. If the text has the word "refund", send it to billing. If it has "crash", send it to technical. That works until a customer writes "I want my money back". No rule matches, and the ticket is lost.
- Option 2: learn from examples. Show the program many tickets, each with its label, and let it find the patterns by itself.
Supervised learning is option 2: flash cards for a program. Examples with answers, and then a new case.
Five words, one picture
- Training data: a table of examples.
- Features: the inputs. Here, the ticket text.
- Label: the known answer next to each example, billing or technical.
- Model: the thing that learns which words go with which label.
- Prediction: what the model gives back for a new ticket it has never seen.
Classification and regression
Supervised learning comes in two kinds, depending on the answer you want.
| Kind | The answer is | Examples |
|---|---|---|
| Classification | a category | a ticket is billing or technical; an email is spam or not spam; a photo shows a cat or a dog |
| Regression | a number | house size predicts the price; shop visits predict next week's sales; engine hours predict a repair cost |
Our ticket sorter picks one of two names, so it's classification. A category answer is classification, a number answer is regression. (For a regression worked end to end, see Linear Regression in scikit-learn.)
Keep some tickets hidden
One more idea before the code, and it's an important one. Say we have twelve labelled tickets. We don't give all of them to the model.
Why bother? Because scoring well on tickets the model has already studied proves very little. A test set shows how it behaves on tickets it has never seen. Our first script below skips this step to keep the code short; how to split properly, with train_test_split, is the subject of Train-Test Split and Data Leakage.
The training data in code
The code uses scikit-learn, a popular Python library for machine learning. Twelve tickets, six billing and six technical, each one a pair: the text and its label.
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.linear_model import LogisticRegression
# Training data: ticket text (the input) and the known answer (the label)
tickets = [
("I was charged twice this month", "billing"),
("Please send me the invoice for March", "billing"),
("My refund has not arrived yet", "billing"),
("Update the card on my payment method", "billing"),
("Why did the price of my plan go up", "billing"),
("Cancel my subscription and stop billing", "billing"),
("The app crashes when I open settings", "technical"),
("I cannot log in, the page keeps loading", "technical"),
("Error message appears when I upload a file", "technical"),
("The website is very slow today", "technical"),
("Password reset email never shows up", "technical"),
("Sync stopped working on my phone", "technical"),
]
texts = [text for text, label in tickets]
labels = [label for text, label in tickets]
The last two lines split the pairs into two lists. texts holds the inputs, labels holds the answers.
Words become numbers, then fit
The model can't read words directly. It needs numbers.
# Turn each text into word counts (the features), then learn from the examples
vectorizer = CountVectorizer()
features = vectorizer.fit_transform(texts)
model = LogisticRegression()
model.fit(features, labels)
CountVectorizer turns each text into word counts. Take the first ticket, "I was charged twice this month": was, charged, twice, this and month each get a count of 1. The single letter "I" is skipped; that's the default. fit_transform does this for all twelve tickets and gives a grid of 12 rows and 65 columns, one column for every word it has seen. Those counts are our features. (Bag of Words with CountVectorizer covers this step in depth.)
Then we create a LogisticRegression model and call fit with the features and the labels. Fit is where the learning happens. Words like charged, month and twice end up leaning billing; sync, stopped and phone lean technical.
And here's a surprise: the word my leans billing too. Four billing tickets say "my", and only one technical ticket does. The model only knows the examples we gave it.
Three tickets it has never seen
# New tickets the model has never seen
new_tickets = [
"I need a refund for the extra charge",
"The app crashes on my phone",
"My invoice shows the wrong price",
]
predictions = model.predict(vectorizer.transform(new_tickets))
for text, answer in zip(new_tickets, predictions):
print(f"{answer:<10} <- {text}")
billing <- I need a refund for the extra charge
technical <- The app crashes on my phone
billing <- My invoice shows the wrong price
Notice the new tickets get vectorizer.transform, not fit_transform: they're turned into counts with the same 65 columns the model learned from. Three tickets it has never seen, three labels, and all three are right.
How sure is it? predict_proba
So why these labels? Add a few lines after the script and ask the model how sure it is. predict_proba returns one probability per class, in the order of model.classes_.
print(model.classes_)
probabilities = model.predict_proba(vectorizer.transform(new_tickets))
for text, probs in zip(new_tickets, probabilities):
print(probs.round(2), text)
['billing' 'technical']
[0.56 0.44] I need a refund for the extra charge
[0.4 0.6] The app crashes on my phone
[0.64 0.36] My invoice shows the wrong price
- "I need a refund for the extra charge": it knows refund, for and the, but need, extra and charge are new. It only ever saw charged, with a d. Billing, at 0.56.
- "The app crashes on my phone": no new words at all. App, crashes and phone all came from technical examples. Technical, at 0.6.
- "My invoice shows the wrong price": it knows invoice, price and my; only wrong is new. Billing, at 0.64.
All three are right, but only just. We gave it twelve examples, and more examples give better answers. For more on reading predict, predict_proba and classes_ together, see Logistic Regression in scikit-learn.
Where it fits in AI engineering
- Routing. A small classifier can decide where a request goes before any large model is called.
- Evaluation. Labelled examples and a test set tell you whether a change made your system better or worse.
- Checks. Classifiers can flag spam, unsafe text or the wrong topic around an AI feature.
The idea is the same every time: examples with answers go in, and a model comes out. When you want a stronger ticket sorter with TF-IDF and a pipeline, the next step is Build a Text Classifier with TF-IDF and a Pipeline.
Common mistakes
- Calling a number problem classification, or the other way round. Ask: is the answer a category or a number?
- Judging a model on the tickets it trained on. Only unseen data tells you the truth.
- Calling
fit_transformon new tickets. New data getstransform, so it uses the same columns as training. - Trusting a prediction without looking at how sure it is. 0.56 and 0.99 both print "billing".
Quick recap
| Word | Meaning | In the ticket sorter |
|---|---|---|
| Features | the inputs | word counts, a 12 x 65 grid |
| Label | the known answer | billing or technical |
| Fit | learns from the examples | model.fit(features, labels) |
| Classification / regression | a category answer / a number answer | classification |
| Test set | new, unseen data | skipped here, see the split article |
Try it yourself
Add the ticket "I want my money back" to new_tickets and run the script again. Then add two or three more labelled billing tickets that talk about money and see how predict_proba changes.
Related course on Udemy
Become an AI Engineer: LLMs, RAG, Agents and MCP
Build real AI systems in Python: LLM APIs, RAG, evals, agents and MCP.View on Udemy ↗: Become an AI Engineer: LLMs, RAG, Agents and MCP (paid course, opens in a new tab)