Categorical Variable

A categorical variable is a variable whose possible values are distinct labels or groups — blood type, country, device brand, smoker or non-smoker, letter grade — so that each observation is assigned to exactly one category from a defined set called its levels. Where a numerical variable asks "how much?", a categorical variable asks "which one?", and the statistics built for it revolve around counts, proportions, and associations between categories.

The decisive feature of a categorical variable is that its values name membership rather than magnitude, and what can be done with it depends on whether the categories carry an order. A nominal categorical variable such as eye color has no ranking, and an ordinal one such as shirt size (small, medium, large) does; those distinctions are treated as measurement scales elsewhere. A variable with exactly two levels is binary or dichotomous (survived or not), and it occupies a special position because coding it as 0 and 1 makes it directly usable in regression, where the mean of the 0/1 values is simply the proportion in the category. The number of distinct levels is its cardinality, and it shapes nearly every practical decision: a variable with three levels and one with fifty thousand (user ids, zip codes, product skus) need entirely different handling.

Summaries and tests for categorical variables use frequency tables, bar charts, and the chi-square family rather than means and standard deviations. The mode, the most common category, is the only measure of center that always applies. Karl Pearson's 1900 chi-square test compares observed counts with those expected under a hypothesis, and its main use in practice is testing whether two categorical variables are independent in a contingency table. The odds ratio and logistic regression, descended from work by Joseph Berkson in the 1940s and later David Cox in 1958, model a binary categorical outcome. For two-category comparisons of proportions the z-test or Fisher's exact test (from Ronald Fisher's well-known 1920s tea-tasting example of a lady claiming to tell milk-first from tea-first) is used when counts are small.

Column: payment_method
card, bank transfer, cash
Encoding for a model
One-hot encoding
three 0/1 columns, one per level
Dummy coding
two columns, one level is the reference
Target or frequency encoding
needed when levels number in the thousands
Model learns an effect per level
relative to the reference category
Pitfall: label encoding 1, 2, 3
invents an order and a distance that do not exist

In machine learning, preparing categorical variables is a routine and consequential step, because most algorithms require numeric inputs. One-hot encoding creates one indicator column per level; dummy coding in linear models drops one level to avoid perfect collinearity (the dummy variable trap); and for high-cardinality variables practitioners use frequency encoding, hashing, learned embeddings, or target encoding, which replaces each level with an average of the outcome and therefore needs care to prevent leakage of the answer into the features. Gradient-boosted tree libraries such as LightGBM and CatBoost handle categorical columns natively. Rare levels are typically pooled into an "other" category, and unseen levels at prediction time must be anticipated.

The standard pitfalls are treating numerals that merely label categories as numbers and ignoring how categories were defined. A zip code, a jersey number, or a diagnosis code stored as an integer invites averages that have no meaning, and integer label encoding of a nominal variable makes a linear model believe that category 3 is "three times" category 1. Categories can also be unstable: merging or splitting levels can reverse an association, which is the structure behind Simpson's paradox, as in the well-known 1973 Berkeley graduate admissions data where aggregated and department-level admission rates told opposite stories. Finally, categories must be mutually exclusive and exhaustive, or counts will double-count or miss observations.

Read a categorical variable as a way of sorting observations into labeled bins: everything it can legitimately say is about membership, proportions, and differences between groups, and the common errors all come from smuggling in a number or an order that the labels never contained.

Take categorical variable further

Ask the copilot about this term, or have our engineers assess it against your process.