Sub topic 1
The Task: Assigning Labels to Documents
Prerequisites
You should know basic Python, including lists, loops, and function calls. You should also understand the difference between a training set and a test set in supervised learning. Basic NumPy and pandas usage helps, but this chapter does not require either library.
What Text Classification Means
Text classification is the task of assigning a category, or label, to a piece of text. A document can be an email, a customer review, a news article, or a support request. The label describes the decision you want a program to make.
For example, a support system might classify incoming messages into these labels:
| Document | Label |
|---|---|
| “The app crashes when I open it.” | technical_problem |
| “How much does the annual plan cost?” | billing_question |
| “I want to cancel my subscription.” | cancellation |
A supervised classification task uses examples where both the text and the correct label are known. These examples form a labeled dataset. The learning algorithm studies patterns in the examples and then predicts labels for new documents.
The labels are the target. The measurable information used to make a prediction is called a feature. Raw text is not directly suitable for most traditional machine-learning algorithms, so a later step converts words into numeric features. TF-IDF is one such conversion.
Defining the Prediction Problem
Before choosing an algorithm, define the decision precisely. Ask:
- What is one document?
- Which labels are possible?
- Is exactly one label correct for each document?
- What should happen when the text is unclear?
- How will success be measured?
In single-label classification, each document receives one label from a fixed set. For example, a news article might be assigned to sports, business, or politics.
In multilabel classification, one document can receive several labels. An article could be both technology and business. Logistic regression can support both settings, but the training and evaluation setup differs. This chapter focuses on single-label classification.
The label definitions must be consistent. If one annotator calls a message “billing” and another calls the same kind of message “account,” the model receives contradictory evidence. Improving the labeling rules can help more than changing the algorithm.
The Learning Workflow
A typical workflow separates data into distinct stages:
The training set is used to learn the relationship between text and labels. The test set contains documents held back until the end. Its labels are hidden from the model during training, so it provides an estimate of performance on unseen text.
A classifier should not learn from the test documents, even indirectly. For example, fitting a vocabulary or calculating word statistics using all documents before splitting can let test information influence training. This is called data leakage. A pipeline, introduced later, helps keep text transformation and model fitting inside the correct boundary.
A minimal example shows the shape of the task:
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
documents = [
"the screen is cracked",
"my invoice has the wrong amount",
"please cancel my account",
"the application will not start",
"how do I update my payment method",
"I want to end my subscription",
]
labels = [
"technical_problem",
"billing_question",
"cancellation",
"technical_problem",
"billing_question",
"cancellation",
]
vectorizer = TfidfVectorizer()
features = vectorizer.fit_transform(documents)
classifier = LogisticRegression(max_iter=1000)
classifier.fit(features, labels)
new_document = vectorizer.transform(["the program crashes on startup"])
print(classifier.predict(new_document)[0])The vectorizer turns text into numbers, and the classifier learns from those numbers and their labels. The predicted result should be technical_problem, but a real system needs many more varied examples before that prediction can be trusted.
A Useful Baseline
TF-IDF with logistic regression is a strong baseline for many text problems because it is fast, interpretable, and effective when word choices distinguish categories. It is a baseline because it gives you a simple reference point. More complex models should demonstrate a clear improvement over it.
Start with a held-out test set and record the class names. Then inspect errors, not only the overall score. Misclassified documents may reveal overlapping labels, missing examples, spelling variation, or a business rule that the model cannot infer from text alone.
Exercise
You are classifying messages as either urgent or normal. A message says, “The service is down for every user.” What makes this a supervised classification example, and what is the target?
Show answer
It is supervised because the system learns from earlier messages paired with known labels. The target is the label, such as urgent or normal, assigned to the new message.