Concepts / Working With String Categories in Tabular Data

Working With String Categories in Tabular Data

An integer column can represent categories, because the storage type says nothing reliable about whether a feature is a quantity.

  • Programming

When Digits Stand for Names

Many datasets are full of numbers that were never meant to be measured. A column might hold 0, 1, 2, and so on, while the thing it describes is a set of named options: a job type, a country, a survey answer. Once the labels have been replaced by digits, the table looks like it contains quantities, and it is easy to forget what the digits actually stand for. This article looks at how that happens, why it causes trouble when a model is trained, and how to decide what kind of feature an integer column really is.

The idea to hold onto is simple. Categorical data is often encoded as integers, either for ease of storage or because of how the data was collected. The meaning of such a column has to be recovered from context. The rest of this article shows how to do that step by step.

The Trap of Trusting the Storage Type

A common assumption goes like this: if a column contains integers, then its values are counts or measurements, so the column is continuous and the numbers can be compared, averaged, and fed to a model as ordinary magnitudes. This belief is the one to watch for. It treats the data type of the stored value as if it revealed the meaning of the feature.

Key point

The storage format and the semantics are two separate questions. The answer to one does not settle the other. An integer type is a fact about storage. The category is a fact about meaning, and only the meaning should guide how the feature is modeled.

Recovering Meaning from Context

It helps to see how the same information can appear in two different forms. In the adult dataset, categorical variables such as workclass were stored as strings like Private. Storing text has one advantage: it clearly marks a variable as categorical. It also has a drawback, because spelling errors become possible. Categorical data is frequently stored as integers instead, either because integers are easier to store or because of the way the data was collected.

In the real world

Consider how a census questionnaire might produce the same information. Suppose each answer for workclass was recorded by position: 0 for the first box ticked, 1 for the second, 2 for the third, and so on up to 8. The column now holds numbers from 0 to 8 instead of readable strings. Someone scanning the table cannot tell from the numbers alone whether the variable should be treated as continuous or categorical. The digits look like the same kind of thing as an age or an income, even though they describe a choice among options.

What resolves the ambiguity is knowing what the numbers mean. Once you know that the codes indicate employment status, it becomes clear that these are very distinct states, and that they should not be modeled by a single continuous variable. The numbers themselves carry no such information. Their meaning lives in the documentation or in your understanding of the data collection, and you have to go and find it.

The Ordering Test

The central question for any integer feature is whether the encoded values have an ordering. Ordering means that moving from one code to another corresponds to a real, meaningful step along some scale. When no such ordering exists between the semantics that are encoded, the feature must be treated as discrete, and in practice it is typically one-hot encoded so that each state gets its own indicator. The workclass example falls into this group, since its codes name employment statuses that do not lie along any scale.

The top row shows an integer feature with a natural order. A rating of four stars is higher than a rating of two, so each step along the row corresponds to a real move on a scale. The bottom row shows codes that only label options. Code 0 is not less than code 1 in any meaningful sense, and the distance between code 0 and code 2 says nothing about how the options relate. A model that receives these numbers as magnitudes will read a meaning into the gaps that was never there.

Tracing a Column Through the Decision

Read the flowchart from the top. The first question is not about the data type at all. It asks what the numbers mean. If the values measure amounts, the feature is a quantity. If they name states, the second question asks whether moving between codes corresponds to a real step along a scale. A no answer leads to a discrete treatment, usually one-hot encoding. A yes answer leads to an ordered feature, which still needs a deliberate encoding choice, as discussed below.

Worked example

Classifying three integer columns

A table has three integer columns. Column A holds the number of visits a customer made last month. Column B holds a code from 0 to 3 for the store region the customer belongs to, where the codes are assigned arbitrarily. Column C holds a satisfaction answer from 1 to 5, where 5 means most satisfied. Classify each column as continuous, discrete, or ordered categories.

  1. 1
    Ask what the numbers mean for column A

    A visit count measures an amount. Moving from 2 visits to 3 is a real, comparable change, so column A is a quantity and can be treated as continuous.

  2. 2
    Ask what the numbers mean for column B

    The codes name regions. They identify options rather than measure amounts, so the feature is categorical. Next ask whether the codes have a meaningful order.

  3. 3
    Test the ordering for column B

    The codes were assigned arbitrarily, so region 2 is not between region 1 and region 3 in any real sense. No ordering exists, so column B is discrete and is typically one-hot encoded.

  4. 4
    Ask what the numbers mean for column C

    Each value is a point on a rating scale, so the feature names states that lie along a scale rather than unrelated labels.

  5. 5
    Test the ordering for column C

    A rating of 4 is higher than a rating of 2, so the ordering is real. Column C is an ordered categorical feature, and the encoding should be chosen with the task and algorithm in mind.

Result

Column A is continuous. Column B is discrete and usually one-hot encoded. Column C is ordered categories whose encoding is a context-dependent choice.

Ordered Integers Still Need a Choice

Not every integer feature is clearly categorical. A five-star rating has a natural order, so the feature is not simply a set of unrelated labels. Even so, the source is careful on this point: for a case like five-star ratings, the better encoding depends on the particular task and data, and on which machine learning algorithm is used. There is no single correct answer that applies everywhere.

QuestionKeeping the order in one numeric columnOne-hot encoding each state
What structure is kept?The sequence of codes, so a higher code stays higherEach state gets its own indicator, so the order is not represented
What structure is discarded?Distances may still be read as meaningful by the modelAny ordering between the states is discarded
When can it fit?When the ordering is real and the task and algorithm can use itWhen the states have no ordering between them, as with the workclass codes

Two encodings for an integer feature with an ordering

Mistakes When Reading Integer Columns

  • Common mistake

    Deciding a column is continuous because its values are integers

    What it looks like

    A column of codes from 0 to 8 is averaged because the values are numbers.

    Why it fails

    The storage type does not reveal whether the feature is a quantity. The question is what the numbers mean.

    Do this instead

    Ask what the codes stand for, and whether they measure amounts or name options.

  • Common mistake

    Assuming any numeric order between codes is meaningful

    What it looks like

    Code 2 is treated as halfway between code 1 and code 3 for an arbitrary set of regions.

    Why it fails

    If there is no ordering between the encoded semantics, the codes do not lie along a scale, so their numeric distances mislead.

    Do this instead

    Apply the ordering test before treating codes as ordered.

  • Common mistake

    Treating an ordered feature as settled once it has an order

    What it looks like

    Five-star ratings are always encoded one way because they are ordered.

    Why it fails

    The better encoding depends on the task, the data, and the algorithm.

    Do this instead

    Treat the ordering as necessary information for the decision, not as the decision itself.

  • Common mistake

    Forgetting that a column of digits may be an encoding of text, so the meaning must be recovered from documentation or context

Predicting the Averaged Code

What do you think happens?

If you average the workclass codes 0 through 8 across all rows, does the result describe a meaningful employment status?

  • Yes, the average is a meaningful status between the two nearest codes

  • No, the codes name states that do not lie along any scale

Reveal answer
Answer:

No, the codes name states that do not lie along any scale

The workclass codes name employment statuses that do not lie along any scale, so there is no meaningful step between them. An average of the codes produces a number that corresponds to no real state, which is why the feature is treated as discrete.

MEDIUM

A dataset contains a column called region_code with values 1 to 4, where each number was assigned to a region in the order the regions were entered into a spreadsheet. Another column called grade holds values 1 to 6, where 6 is the highest grade. Decide how each column should be treated, and justify your answer using the ordering rule rather than the storage type.

Hints
  • Start by asking what the numbers mean, not what type they are stored as.
  • Ask whether the codes were assigned for a real reason to be ordered, or only by the order of entry.
  • For grade, check whether moving up one code corresponds to a real step on a scale.

What to Carry Forward

  1. 1
    An integer column can represent categories, because the storage type says nothing reliable about whether a feature is a quantity.
  2. 2
    Categorical data is often encoded as integers for ease of storage or because of how the data was collected, so the meaning must be recovered from context.
  3. 3
    If the encoded states have no ordering between them, treat the feature as discrete, usually one-hot encoded.
  4. 4
    Ordered integer features such as five-star ratings do not have a single correct encoding; the choice depends on the task, the data, and the algorithm.

Key Takeaways

  • An integer column can represent categories, because the storage type says nothing reliable about whether a feature is a quantity.
  • Ask what the numbers mean before deciding how to model them; the meaning lives in context, not in the data type.
  • If the encoded states have no ordering, treat the feature as discrete and usually one-hot encode it.
  • Ordered integer features still require a context-dependent encoding choice that depends on the task, the data, and the algorithm.

Related concepts

More concepts starting with W