Concepts / Continuous and Categorical Features

Continuous and Categorical Features

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.

  • Programming

A Spam Filter Meets a Sentence

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 holds 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.

What do you think happens?

Of these three feature values, which one can be passed straight into a machine learning algorithm without any further preparation: 4.5, the category red from a fixed list of colors, or the sentence Win a free prize now?

  • The number 4.5

  • The category red

  • The sentence Win a free prize now

Reveal answer
Answer:

The sentence is the one that cannot be used directly. Machine learning algorithms cannot directly handle raw text, so it has to be preprocessed first.

The number and the category are the two feature kinds the course has already covered. The sentence is text, which is a third kind of feature with a different shape. The rest of this article explains that difference and what preparation it requires.

Three Kinds of Feature Side by Side

Before looking at text in detail, recall the two feature types already covered. A continuous feature describes a quantity. A categorical feature is an item from a fixed list. Text data is a third kind. It is not a number that can be compared or averaged, and it is not one of a fixed set of labels. Each kind of feature asks a different question about the data, and the difference determines how the data must be handled.

Continuousdescribes a quantityNumbers that can becompared or averagedfixed kind of value perexampleCategoricalitem from a fixed listOne label from a setof labelsfixed kind of value perexampleTexta string of charactersStrings of variablelengthnot a number, not a fixedlabelvaluevaluevalue
What each of the three feature types represents and what kind of value it holds for each example.

The comparison shows the key distinction. Continuous and categorical features each have a fixed kind of value per example. 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.

Tracing Lengths of Raw Strings

A text feature is represented as a string made of characters, and strings have variable length. To see what that means, follow three values through the same check. The table below uses invented example strings and counts their characters, including spaces. Each row is one example from a hypothetical email dataset.

ExampleRaw valueNumber of characters
Email 1Hi2
Email 2Win a prize11
Email 3Meeting moved to Thursday afternoon35

Three invented text values of different lengths. Length varies from example to example, which a single number or a single category does not do.

Every row holds one value for one example, just as a continuous or categorical feature would. But the value itself is a sequence of characters whose size changes from row to row. A continuous feature gives each example a single quantity, and a categorical feature gives each example one item from a list. The text feature gives each example a string whose shape is different in each case. This is the practical meaning of saying text has variable length.

Why Raw Text Cannot Go Straight In

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 stages below show the order in which that conversion moves a value from raw text toward something an algorithm can use.

Raw textstring of charactersPreprocessingconverts text into astructured formatStructured formatsuitable for analysisMachine learningalgorithmapplied after preprocessinginputoutputused by
The order of stages that a raw text value passes through before a machine learning algorithm is applied to it.

The pipeline makes one point clear. Raw text is not the input the algorithm receives. Preprocessing sits between them, and it is a required stage rather than an optional refinement. The source does not describe the individual steps inside preprocessing, so this article does not list them.

Recognizing a Text Feature

The source gives examples where text carries the information a decision needs: the content of an email for spam detection, speeches or tweets for learning a politician's opinion, and the subject line and content of a message for a customer service system. In each case the information lives in the words themselves. A rough check that follows from the definitions is to ask three questions of the values. Does each example hold a quantity that can be compared or averaged? Does each example hold one item from a fixed list of labels? If neither is true, and each value is a string of characters, the feature is text.

Mistakes Beginners Make With Text Features

  • Common mistake

    Treating a text value as if it were a number because it is stored in a column of a table

    What it looks like

    Passing a column of email contents directly to an algorithm as though each row were a single quantity

    Why it fails

    Text is a string of characters with variable length. It is not a number that can be compared or averaged.

    Do this instead

    Recognize the column as text and send it through preprocessing before the algorithm is applied.

  • Common mistake

    Assuming text must be handled exactly like a categorical feature because both are given as words

    What it looks like

    Treating a whole email body as one label chosen from a fixed list

    Why it fails

    A categorical feature is an item from a fixed list. Email content is not chosen from a fixed set of labels, and its length varies.

    Do this instead

    Ask whether the values come from a fixed list of labels. If they do not, the feature is text.

  • Common mistake

    Believing the source explains how to preprocess text step by step

    What it looks like

    Looking for the specific preprocessing methods in the course material on text features

    Why it fails

    The source states that preprocessing is required but does not provide detailed best practices for building pipelines.

    Do this instead

    Learn the necessity and purpose of preprocessing from this material, then consult a fuller source for specific techniques.

Four Kinds of String Data

The section introduces four kinds of string data that you may encounter in practice. The supplied material announces these four kinds but does not list or describe them. Because of that, 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 that the surrounding material develops.

Practice Classifying Features

EASY

For each feature below, decide whether it is continuous, categorical, or text. Feature A: the temperature of a room in degrees. Feature B: the body of a customer complaint email. Feature C: the department a worker belongs to, chosen from a fixed list of departments. Give a short reason for each answer based on whether the values measure a quantity, come from a fixed list, or are strings of characters.

Hints
  • Ask first whether the values measure a quantity that can be compared or averaged.
  • Then ask whether each value is one item from a fixed list.
  • A value that is a string of characters with variable length points toward text.

Summary of Text as a Feature Type

At a glance

  • 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 data is represented as strings made of characters, and those strings have variable length, which is why it cannot simply be dropped into an algorithm.
  • Machine learning algorithms cannot directly handle raw text, so it must be preprocessed into a structured format suitable for analysis before it is used.
  • The source announces four kinds of string data but does not list or describe them, so their details should be checked in a fuller source.

Related concepts

More concepts starting with C