Week 2: Categorical Data & Visual Summaries

3107 words
16 min read
2026-09-12T00:00:00.000Z
Python Week 1: the first filter for runtime behavior
Visual companion
Python
Type and operator map

Python Week 1: the first filter for runtime behavior

View
Revision summary

What this note is really saying

Short form

Frequency distributions, relative frequencies, proportions, mode, bar charts, pie charts, and contingency tables. # Statistics for Data Science I — Week 2: Categorical Data & Visual Summaries IITM’s Week 2 moves into **categororical data and its summaries/representations**.

Statistics for Data Science I — Week 2: Categorical Data & Visual Summaries

IITM’s Week 2 moves into categororical data and its summaries/representations.
The syllabus line is broad, so the detailed treatment below is a standard expansion of that topic rather than extra IITM-specific claims.

TL;DR

For categorical data, the main job is not to calculate means.
It is to answer:
How many observations belong to each category?\boxed{ \text{How many observations belong to each category?} }
and:
What fraction/percentage belongs to each category?\boxed{ \text{What fraction/percentage belongs to each category?} }
The core tools are:
  • frequency
  • relative frequency
  • percentage
  • frequency tables
  • bar charts
  • pie charts
  • comparing categorical distributions
  • two-way/contingency tables for two categorical variables

1. Recap: categorical variable

A categorical variable records group membership or labels.
Examples:
  • blood group
  • city
  • operating system
  • favorite sport
  • education level
  • satisfaction category
Its values describe categories rather than measurements.

2. Nominal vs ordinal

Two major categorical types:

Nominal

Categories have no intrinsic order.
Example:
{Android,iOS,Other}\{\text{Android},\text{iOS},\text{Other}\}
There is no natural:
Android<iOS.\text{Android}<\text{iOS}.

Ordinal

Categories have meaningful order.
Example:
Poor<Average<Good<Excellent.\text{Poor}<\text{Average}<\text{Good}<\text{Excellent}.
But spacing between categories is not necessarily equal.

3. Frequency

The frequency of a category is the number of observations belonging to it.
Suppose 20 students choose a preferred programming language:
LanguageFrequency
Python10
Java5
C3
Other2
Total:
10+5+3+2=20.10+5+3+2=20.

4. Frequency notation

If category ii occurs:
fif_i
times, then:
∑ifi=n\boxed{ \sum_i f_i=n }
where:
nn
is the total number of observations.
This is an important sanity check.

5. Relative frequency

Raw counts are often hard to compare across datasets of different sizes.
Relative frequency solves that.
For category ii:
Relative frequency=fin\boxed{ \text{Relative frequency} = \frac{f_i}{n} }
It lies between:
00
and:
1.1.

6. Example

Using:
LanguageFrequency
Python10
Java5
C3
Other2
with:
n=20,n=20,
Python relative frequency:
1020=0.5.\frac{10}{20}=0.5.
Java:
520=0.25.\frac5{20}=0.25.
C:
320=0.15.\frac3{20}=0.15.
Other:
220=0.10.\frac2{20}=0.10.

7. Relative frequencies must sum to 1

Because:
∑ifi=n,\sum_i f_i=n,
dividing everything by nn gives:
∑ifin=1\boxed{ \sum_i\frac{f_i}{n}=1 }
Small deviations may occur because of rounding.

8. Percentage frequency

Percentage is simply:
Percentage=Relative frequency×100\boxed{ \text{Percentage} = \text{Relative frequency}\times100 }
For Python:
0.5×100=50%.0.5\times100=50\%.

9. Frequency table

A useful categorical summary might include:
CategoryFrequencyRelative frequencyPercentage
Python100.5050%
Java50.2525%
C30.1515%
Other20.1010%
Total201.00100%
This converts raw categorical observations into a readable distribution.

10. Distribution of a categorical variable

The distribution tells us how observations are spread across categories.
For categorical data, this is essentially the collection of:
category+frequency/proportion\boxed{ \text{category}+\text{frequency/proportion} }
Example:
text
Python  → 50%
Java    → 25%
C       → 15%
Other   → 10%

11. Mode

For categorical data, the most natural measure of "center" is the mode.
The mode is the category with highest frequency.
In the previous example:
Mode=Python\boxed{\text{Mode}=\text{Python}}
because it appears most often.

12. Why mean is usually meaningless for categories

Suppose:
1=Windows1=\text{Windows} 2=macOS2=\text{macOS} 3=Linux.3=\text{Linux}.
Calculating:
xˉ=2.1\bar{x}=2.1
has no meaningful interpretation.
The numbers are labels.
So for nominal categories:
frequencies, proportions and mode are meaningful; arithmetic means usually are not.

13. Bar chart

A bar chart represents each category using a bar whose height corresponds to:
  • frequency
  • relative frequency
  • percentage
Conceptually:
text
Python  ██████████
Java    █████
C       ███
Other   ██
The bars are separated because the categories are distinct.

14. Bar-chart axes

Typically:
  • horizontal axis → categories
  • vertical axis → frequency / proportion / percentage
Example:
x=categoryx=\text{category} y=frequencyy=\text{frequency}

15. Why bars are separated

Categorical values are distinct groups.
There is no continuous numerical interval between:
Python\text{Python}
and:
Java.\text{Java}.
So bars in an ordinary categorical bar chart are usually separated.
This becomes useful when comparing bar charts with histograms later.

16. Bar-chart ordering

For nominal categories, order may be chosen for clarity:
  • alphabetical
  • descending frequency
  • logical grouping
For ordinal categories, preserve the meaningful natural order.
Example:
Poor,Fair,Good,Excellent.\text{Poor},\text{Fair},\text{Good},\text{Excellent}.
Reordering those randomly would destroy information.

17. Frequency vs proportion bar chart

Suppose two datasets have:
n1=100n_1=100
and:
n2=10,000.n_2=10,000.
Raw frequencies may be misleading when comparing them.
Proportions:
fin\frac{f_i}{n}
make comparisons easier.

18. Pie chart

A pie chart represents a whole as:
360∘.360^\circ.
Each category gets an angle proportional to its relative frequency.
For category ii:
θi=fin×360∘\boxed{ \theta_i= \frac{f_i}{n}\times360^\circ }

19. Example

If Python represents:
50%50\%
of observations:
θ=0.5×360∘=180∘.\theta= 0.5\times360^\circ = 180^\circ.
Java at:
25%25\%
gets:
90∘.90^\circ.

20. Pie-chart sanity check

All slices must total:
360∘\boxed{360^\circ}
and percentages must total approximately:
100%.\boxed{100\%}.

21. Bar chart vs pie chart

Both can summarize categorical distributions.

Bar chart

Better for:
  • comparing categories precisely
  • many categories
  • similar-sized categories
  • comparing multiple groups

Pie chart

Useful mainly for:
  • simple part-to-whole representation
  • small number of categories
Bar charts are generally easier for precise comparisons because humans compare lengths better than angles.

22. Pareto-style ordering

A bar chart can sometimes be sorted from largest frequency to smallest:
text
A █████████
C ██████
B ███
D █
This quickly exposes dominant categories.
This sorting is useful for nominal categories, but not when the ordinal order itself carries meaning.

23. Two categorical variables

So far we considered one variable.
Now suppose each observation has:
  • preferred OS
  • device type
For example:
PersonOSDevice
1AndroidPhone
2iOSPhone
3WindowsLaptop
4macOSLaptop
We may want to study whether the two variables are associated.

24. Two-way table

A two-way table, also called a contingency table, summarizes two categorical variables simultaneously.
Example:
PhoneLaptopTotal
Android40545
iOS35237
Windows23032
macOS12526
Total7862140
Each cell contains a joint frequency.

25. Joint frequency

A joint frequency counts observations satisfying both categories.
Example:
4040
means:
40 observations are both Android users and Phone users.

26. Marginal frequencies

The totals along the margins are called marginal frequencies.
From the table:
Android total:
4545
Phone total:
78.78.
These summarize one variable without conditioning on the other.

27. Joint relative frequency

If total observations:
n=140,n=140,
then the joint relative frequency of:
Android and Phone
is:
40140≈0.286.\frac{40}{140} \approx0.286.
So about:
28.6%.28.6\%.

28. Conditional distribution

Suppose we want:
Among phone users, what fraction use Android?
Phone total:
78.78.
Android-phone users:
40.40.
Therefore:
4078\boxed{ \frac{40}{78} }
This is conditional because we restrict attention to one subgroup first.

29. Conditional denominator rule

This is one of the biggest practical traps.
If the question says:
Among laptop users, what fraction use Windows?
Then denominator must be:
number of laptop users,\text{number of laptop users},
not total sample size.
Always ask:
What group am I conditioning on?
That group supplies the denominator.

30. Example

From:
PhoneLaptopTotal
Android40545
iOS35237
Windows23032
macOS12526
Total7862140
Among laptop users, proportion using Windows:
3062≈0.484.\frac{30}{62} \approx0.484.
About:
48.4%.48.4\%.

31. Reverse conditional is different

Compare:
Among laptop users, what percentage use Windows?
3062.\frac{30}{62}.
with:
Among Windows users, what percentage use laptops?
3032.\frac{30}{32}.
These are completely different questions.
So:
P(A∣B)≠P(B∣A)P(A\mid B) \neq P(B\mid A)
in general.
Probability notation comes later, but the denominator intuition begins here.

32. Association between categorical variables

Two categorical variables are associated if the distribution of one changes across categories of the other.
Example:
Suppose:

Phone users

  • Android: 51%
  • iOS: 45%
  • others: 4%

Laptop users

  • Android: 8%
  • iOS: 3%
  • Windows: 48%
  • macOS: 40%
The distributions are dramatically different.
That suggests an association between:
  • device type
  • operating system

33. No association intuition

If conditional distributions look almost identical across groups, that suggests little or no association.
For example:
GroupYesNo
A60%40%
B61%39%
The distributions are very similar.
That does not prove independence formally yet, but it is the relevant descriptive idea.

34. Segmented / stacked bar charts

Two-way categorical data can be visualized with segmented bars.
Example conceptually:
text
Group A |████████████------|
Group B |██████------------|
Each bar can represent:
100%100\%
with different segments representing category proportions.
This makes conditional distributions visually comparable.

35. Side-by-side bar charts

Another option:
text
           Group A   Group B
Yes        ██████    █████
No         ███       ████
This is often easier for comparing individual categories between groups.

36. Counts vs percentages in two-way tables

Suppose:
Group A has:
10001000
people.
Group B has:
100100
people.
Raw counts can obscure relative behavior.
If:
600600
A-members choose Yes and:
7070
B-members choose Yes,
raw counts say:
600>70.600>70.
But percentages say:
60%60\%
vs:
70%.70\%.
So B actually has the higher Yes rate.

37. Simpson's-paradox intuition

A subtle issue can occur when aggregated proportions show one trend while subgroup proportions show another.
This is known as Simpson's paradox.
You do not need the full machinery yet, but retain this lesson:
Aggregation can hide important subgroup structure.
So always inspect whether another categorical variable is mixing together very different groups.

38. Missing category

Suppose a survey includes options:
  • Yes
  • No
  • No response
Do not silently discard "No response" unless the analysis explicitly defines how missing data are treated.
Otherwise the denominator may change.
This can materially affect percentages.

39. Mutually exclusive categories

For a simple one-variable frequency table, categories should generally be mutually exclusive:
each observation belongs to exactly one category.
Bad categories:
  • age <30<30
  • age 2020–4040
Someone aged 2525 belongs to both.
The categories overlap.

40. Exhaustive categories

Ideally, the categories should also cover every possible observation.
Example:
If asking transport mode:
  • car
  • bus
  • bike
but someone walks, there is nowhere to classify them.
An "Other" category may make the scheme exhaustive.

41. Mutually exclusive + exhaustive

A clean categorical classification often aims for:
mutually exclusive+collectively exhaustive\boxed{ \text{mutually exclusive} + \text{collectively exhaustive} }
Meaning:
  • no observation belongs to multiple categories
  • every observation has somewhere to go

42. Frequency table from raw data

Suppose responses are:
text
A, B, A, C, B, A, A, C, B, A
Count:
A=5A=5 B=3B=3 C=2.C=2.
Total:
10.10.
Relative frequencies:
A=510=0.5A=\frac5{10}=0.5 B=310=0.3B=\frac3{10}=0.3 C=210=0.2.C=\frac2{10}=0.2.

43. Recovering frequency from percentage

Suppose:
n=500n=500
and:
30%30\%
belong to category A.
Frequency:
0.30×500=150.0.30\times500=150.
So:
fA=150\boxed{f_A=150}

44. Recovering total from frequency and relative frequency

Suppose category A has:
fA=40f_A=40
and relative frequency:
0.20.0.20.
Since:
40n=0.20,\frac{40}{n}=0.20,
we get:
n=400.20=200.n= \frac{40}{0.20} = 200.

45. Recovering missing frequency

Suppose:
CategoryFrequency
A15
B20
C?
D25
Total:
80.80.
Then:
C=80−(15+20+25)C=80-(15+20+25) =20.=20.
This kind of table arithmetic is straightforward but common.

46. Mode from a frequency table

If:
CategoryFrequency
A10
B17
C9
then:
Mode=B\boxed{\text{Mode}=B}
because:
1717
is the largest frequency.

47. Multiple modes

If two categories tie for highest frequency:
CategoryFrequency
A12
B12
C4
then both:
AA
and:
BB
are modal categories.
The distribution can be called bimodal in an appropriate descriptive sense.

Common mistake: frequency vs relative frequency
Frequency:
count\boxed{\text{count}}
Relative frequency:
counttotal\boxed{\frac{\text{count}}{\text{total}}}
Percentage:
100×relative frequency\boxed{ 100\times\text{relative frequency} }
Don't mix units.

Common mistake: percentages exceeding 100%
For mutually exclusive exhaustive categories:
∑i%=100%\sum_i \%=100\%
approximately.
If the sum is:
140%,140\%,
something is wrong unless multiple responses per observation are permitted.

50. Multiple-response questions

A survey may say:
Select all programming languages you use.
One person may select:
  • Python
  • JavaScript
  • SQL
Then category counts can sum to more than:
n.n.
And percentages may exceed:
100%100\%
when summed.
So always check whether the categories are mutually exclusive.

Common mistake: pie chart for overlapping categories
Pie charts assume parts form one whole.
If respondents can belong to multiple categories, ordinary pie-chart slices are misleading because percentages need not total:
100%.100\%.
Common mistake: treating ordinal values as nominal
For:
Low,Medium,High,\text{Low},\text{Medium},\text{High},
the order itself is information.
A graph should normally preserve:
Low→Medium→High.\text{Low}\to\text{Medium}\to\text{High}.
Alphabetically rearranging them would obscure the structure.

Common mistake: comparing counts with unequal group sizes
Suppose:
Group A:
500/1000500/1000
say Yes.
Group B:
80/10080/100
say Yes.
Counts:
500>80.500>80.
But rates:
50%<80%.50\%<80\%.
For comparisons across differently sized groups, proportions are usually more informative.

Common mistake: wrong conditional denominator
Question:
Among students who passed, what percentage are in Group A?
Denominator:
all students who passed\boxed{\text{all students who passed}}
not:
all students\text{all students}
and not:
all Group A students.\text{all Group A students}.
Read "among..." carefully.

Common mistake: association implies causation
Suppose device type and OS are associated.
That does not automatically mean one causes the other in the statistical causal sense.
Descriptive association tells us distributions differ.
Causal claims require additional assumptions/design.

56. Categorical-data workflow

For one categorical variable:
text
Raw categories
      ↓
Count occurrences
      ↓
Frequency table
      ↓
Relative frequency / percentage
      ↓
Bar chart / simple visualization
      ↓
Identify dominant/rare categories
For two categorical variables:
text
Two variables
      ↓
Contingency table
      ↓
Joint + marginal frequencies
      ↓
Conditional percentages
      ↓
Compare distributions
      ↓
Assess descriptive association

57. Formula sheet

Relative frequency

ri=fin\boxed{ r_i=\frac{f_i}{n} }

Percentage

pi=fin×100\boxed{ p_i= \frac{f_i}{n}\times100 }

Frequency from percentage

fi=pi100n\boxed{ f_i= \frac{p_i}{100}n }

Pie-chart angle

θi=fin×360∘\boxed{ \theta_i= \frac{f_i}{n}\times360^\circ }

Frequency total

∑ifi=n\boxed{ \sum_i f_i=n }

Relative-frequency total

∑iri=1\boxed{ \sum_i r_i=1 }

58. Week 1 → Week 2 connection

Week 1 asked:
What kind of variable is this?\boxed{ \text{What kind of variable is this?} }
If the answer is:
categorical,\text{categorical},
Week 2 asks:
How are observations distributed across its categories?\boxed{ \text{How are observations distributed across its categories?} }
Then with two categorical variables:
Does that distribution change across groups?\boxed{ \text{Does that distribution change across groups?} }
That is the first step toward studying association.

59. What you should actually master

By the end of Stats 1 Week 2, you should be able to:
  1. Identify nominal and ordinal categorical variables.
  2. Build a frequency table from raw data.
  3. Calculate relative frequencies.
  4. Calculate percentages.
  5. Check that frequencies/proportions total correctly.
  6. Identify the modal category.
  7. Read and interpret bar charts.
  8. Understand pie-chart proportions.
  9. Choose proportions instead of raw counts when group sizes differ.
  10. Construct/read a two-way table.
  11. Identify joint and marginal frequencies.
  12. Calculate simple conditional proportions.
  13. Choose the correct denominator in "among..." questions.
  14. Compare categorical distributions across groups.
  15. Describe association without automatically claiming causation.
If those are comfortable, Stats 1 Week 2 is under control.

Quick Recall & Flashcards

Test your active recall before proceeding to practice problems. Format:
Question :: Answer.
  • Week 2 Diagnostic: In a dataset of 50 students, 20 prefer Python, 15 prefer Java, and 15 prefer C++. What is the relative frequency of students who prefer Python? :: Answer: 0.4 — Relative frequency = count / total = 20 / 50 = 0.4.
  • Week 2 Diagnostic: Which measure of central tendency is valid for nominal categorical data? :: Answer: C — Nominal data has no ordering or numerical values, so only the most frequent category (Mode) is meaningful.

Practice & Self-Check

Attempt these diagnostic questions to verify your operational understanding.
1

Question 1

In a dataset of 50 students, 20 prefer Python, 15 prefer Java, and 15 prefer C++. What is the relative frequency of students who prefer Python?
NAT
3 Unit Assessment
2

Question 2

Which measure of central tendency is valid for nominal categorical data?
MCQ
3 Unit Assessment
A. Mean
B. Median
C. Mode
D. Variance

Active Recall Session

Document outline

Keep your place and jump directly to a heading.

Table of contents

No note selected

Open a chapter to see its outline, backlinks, and sources here.