Menu
CoddyTech

The Most Common Value

Part of the Statistics for ML section of Coddy's Machine Learning journey. Lesson 5 of 64.

T-shirt sizes are labels: there is no mean of M and XL. A category column's center is its mode, the most common value:

Bars counting 8 T-shirt orders by size: S 2, M 3, L 2, XL 1. The tallest bar, M, is dark and labelled mode

.mode() finds it:

sizes = pd.Series(["M", "L", "M", "S", "XL", "M", "L", "S"])
print(sizes.mode()[0])

This prints:

M

Values can tie, so .mode() returns a Series of every winner, sorted. [0] takes the first.

The mode fills gaps in a category column. It is also the simplest model of all: always guess the most common class.

challenge icon

Challenge

Easy

A ski shop wants to know what to keep in stock. rentals.csv logs 30 rentals with the columns item, boot_size, level and days.

  1. For each column, in order, print its name, its mode, and how many rentals have that value.
  2. Then compare the mean of days, rounded to 1 decimal, with its mode.

For a shorter log of 8 rentals:

item: snowboard (5 of 8)
boot_size: 42 (4 of 8)
level: beginner (5 of 8)
days: 1 (4 of 8)
Days rented, mean vs mode: 2.8 vs 1

No rental lasts 2.8 days: the mode is a value that really happens.

Try it yourself

import pandas as pd

rentals = pd.read_csv("rentals.csv")

# Write your code below
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Statistics for ML

Practice on your own: Online Python compiler