Chapter 7 Working with Text Data
Text is a third kind of feature, distinct from continuous features that describe a quantity and categorical features that are items from a fixed list.
A Spam Filter That Reads Words
Most of the features you have worked with so far are either numbers that measure a quantity or items chosen from a fixed list. Many real applications, however, contain a third kind of information: text. Consider a system that must decide whether an email is legitimate or spam. The content of the email will almost certainly carry information that matters for that decision. The same is true when someone wants to learn a politician's opinion on immigration from speeches or tweets, or when a customer service system must decide whether a message is a complaint or an inquiry from its subject line and content.
This article focuses on the step that comes before learning. It asks two questions: how is text data characterized as a feature, and why does it have to be prepared before a machine learning algorithm can use it? Along the way you will see how text differs from the two feature types you already know, and what the source does and does not tell you about the kinds of string data that appear in practice.
Two Feature Types the Course Already Covers
Before looking at text in detail, recall the two feature types covered so far. A continuous feature describes a quantity. A categorical feature is an item from a fixed list. Each kind of feature asks a different question about the data, and the difference determines how the data must be handled.
Continuous feature: a feature that describes a quantity, such as a value that can be compared or averaged.
Categorical feature: a feature whose value is an item chosen from a fixed list of labels.
Text as a Third Feature Type
Text feature: a feature whose values are strings made of characters. Text is a third kind of feature. It is not a number that can be compared or averaged, and it is not one of a fixed set of labels.
The key distinction lies in the shape of each value. Continuous and categorical features each have a fixed kind of value per example: a quantity in one case, a label from a fixed list in the other. Text features are strings, and those strings can differ widely in length. That variable shape is the reason text cannot simply be dropped into an algorithm the way a number can.
Read the diagram column by column. Each feature type has its own shape of value. The continuous and categorical columns both have one fixed kind of value per example, while the text column is defined by strings whose length varies. This is the structural difference that makes text a separate case rather than a special type of number or category.
Tracing What One Column Looks Like
Classifying three values from one column
Suppose a single column in a dataset holds the values 42, Tuesday, and 'Where is my order, it still has not arrived?'. Decide which feature type each value belongs to, using the definitions in this article.
- 1Test the first value as a quantity
42 is a number that measures a quantity, so it fits the continuous definition. Fixed-list membership is not the question here.
- 2Test the second value against a fixed list
Tuesday is one item from the fixed list of days of the week. That makes it a categorical value.
- 3Test the third value as a string
The third value is a string of characters whose length varies. It is not a quantity and it is not one item from a fixed set of labels, so it is text.
- 4Notice the mixed column
A single column of this kind mixes three shapes of value. In practice, the column's meaning determines its feature type, and the values should be checked to confirm they share one kind.
42 is continuous, Tuesday is categorical, and the complaint sentence is text. The third value is the one that needs preprocessing before an algorithm can use it.
What do you think happens?
Is a free-text subject line like 'Refund request, item damaged' a continuous, categorical, or text feature?
Reveal answer
Text
The subject line is a string of characters whose length can vary from message to message. It is not a quantity that can be compared or averaged, and it is not one item from a fixed list of labels.
Why Raw Text Must Be Preprocessed
Machine learning algorithms cannot directly handle raw text. Preprocessing is the work of converting raw text into a structured format suitable for analysis. The source describes this as a necessary step: the text has to be processed before any algorithm is applied to it.
The pipeline shows the order of the stages at the level the source supports. Raw strings enter preprocessing, which produces a structured format, and only that structured format is passed to an algorithm. The source does not name the individual sub-steps inside preprocessing, so this article does not list them.
Deciding Whether a Column Is Text
When you are handed a column and must decide how to treat it, you can ask the same questions the definitions answer. The flowchart below turns those questions into an order to check them in.
Follow the flowchart from the top. The first question separates quantities from everything else. The second separates fixed-list labels from strings. The third identifies text. Work through the questions in this order because a quantity can look like a string when it is stored as characters, and the definitions are what decide the feature type.
Mistakes Beginners Make with Text Features
Common mistake Feeding a raw text column directly into a machine learning algorithm
What it looks likePassing a column of complaint messages straight into a model without any preparation.
Why it failsMachine learning algorithms cannot directly handle raw text, so the text must first be converted into a structured format.
Do this insteadRoute the raw strings through preprocessing and pass only the structured result to the algorithm.
Common mistake Treating text as if it had the shape of a number or a category
What it looks likeAssuming that every value in a message column has a fixed kind and can be compared or averaged like a quantity.
Why it failsText values are strings whose length can differ widely, which is the variable shape that separates text from continuous and categorical features.
Do this insteadIdentify the column as text using the definitions, then prepare it before modeling.
Common mistake Assuming the four kinds of string data are fully described in this material
Four Kinds of String Data
The material introduces four kinds of string data that you may encounter in practice. It announces these four kinds but does not list or describe them, so this article does not name them or state how they differ. What can be said with confidence is that string data is the general form of the text features discussed above, and that the four kinds are a further classification developed by the surrounding material. Their details should be checked in a fuller source.
Practice Classifying Feature Columns
For each column below, state whether it should be treated as continuous, categorical, or text, and explain your choice using the definitions from this article. Column A: a list of house prices in dollars. Column B: a list of country names chosen from a fixed registry. Column C: free-form product reviews written by customers.
Hints
- Ask first whether the values are quantities that can be compared or averaged.
- A fixed registry is a fixed list of labels, so think about whether the values are chosen from that list.
- Free-form reviews are strings of characters whose length varies from one review to the next.
Key Takeaways
- Text is a third kind of feature, distinct from continuous features that describe a quantity and categorical features that are items from a fixed list.
- Text values are strings made of characters, and those strings have variable length, which is why text cannot be dropped directly into an algorithm.
- Machine learning algorithms cannot directly handle raw text, so preprocessing must convert it into a structured format suitable for analysis before any algorithm is applied.
- The source announces four kinds of string data but does not list or describe them, so their details must be checked in a fuller source.