liveBook · Manning

Categorical data

As explained in Statistics Every Programmer Needs

A categorical variable is a variable whose values represent membership in distinct qualitative classes or categories.

Categorical data records membership in distinct qualitative classes rather than measured numerical quantities. Summarize its distribution with frequency counts and proportions before applying deeper analysis. section 1.1.4

When two categorical variables are analyzed together, organize their class combinations in a contingency table. Rows and columns represent classes, while each cell records the corresponding frequency. section 3.3.3

Use categorical data to:

  • Count how frequently each class occurs. section 1.1.4
  • Calculate proportions within a category or across a contingency-table margin. section 3.3.3
  • Compare combinations of classes, such as symptoms and medical-test results. section 3.3.3

Medical-test contingency table

Represent symptoms as rows and test results as columns: No symptoms with Negative = 860, No with Positive = 90, Yes with Negative = 10, and Yes with Positive = 40. Among symptomatic cases, 40 of 50 are positive, giving an 80% conditional probability. section 3.3.3

>>> import pandas as pd
>>> contingency_table = pd.DataFrame(test_results, index = symptoms)
>>> print(contingency_table)
     Negative  Positive
No        860        90
Yes        10        40

Inspect the pandas contingency table showing categorical classes as labeled rows and columns with frequency counts in each cell. section 3.3.3

How Statistics Every Programmer Needs covers it

How deep it goes
Defines classes and counts, then works through contingency tables and conditional probabilities without extending into advanced modeling.
Examples
Uses symptoms and medical-test results, represented as labeled rows, columns, and frequency-count cells.
What it assumes
Assumes basic data and probability literacy while introducing categorical classes, frequency counts, proportions, and contingency tables.
What you can do after
Apply it via pandas by constructing contingency tables and calculating category-specific proportions.
How it connects
Connects categorical data to descriptive statistics, contingency tables, probability, and conditional probability.

Also explained in

Compare how 16 books explain categorical data →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Statistics Every Programmer Needs. The section links point to the parts of the book it draws on.