Mathematical Concept of Mode

1. Definition

The mode is the value that appears most frequently in a dataset.
In other words, it represents the most common observation — the value that occurs with the highest frequency.

Unlike mean and median, which require numerical order, the mode is meaningful for both numerical and categorical data.


2. Formula and Representation

There is no explicit formula for mode like mean or median; it’s determined by counting frequencies.

Let \(f(x_i)\) denote the frequency (number of occurrences) of each observation \(x_i\).

\[\text{Mode} = x_i \text{ such that } f(x_i) \text{ is maximum}\]

3. Types of Datasets Based on Mode

Type Description Example
Unimodal One mode [2, 3, 3, 4, 5] → Mode = 3
Bimodal Two modes [1, 1, 2, 3, 3] → Modes = 1, 3
Multimodal More than two modes [1, 1, 2, 2, 3, 3]
No mode All values unique [1, 2, 3, 4, 5]

4. Example 1 — Simple Dataset

Dataset: [4, 2, 4, 3, 2, 4, 1]

Frequencies:

Value Count
1 1
2 2
3 1
4 3

Mode = 4, because it appears most frequently.


5. Example 2 — Bimodal Dataset

Dataset: [2, 3, 3, 4, 4, 5]

Frequencies:

Value Count
2 1
3 2
4 2
5 1

Modes = 3 and 4 (both appear twice).


6. Usefulness

  • For categorical data (like colors, cities, brands, etc.) where mean/median make no sense.
  • For identifying most common choices or behaviors.
  • To describe nominal-level data (non-numeric labels).

7. Relation to Distribution Shape

Distribution Type Relationship
Symmetrical Mean = Median = Mode
Positively Skewed Mode < Median < Mean
Negatively Skewed Mean < Median < Mode

Thus, mode helps describe distribution skewness along with mean and median.


8. Limitation

  • Mode might not be unique (multiple modes).
  • It can be unstable if small changes in data alter frequency counts.
  • For continuous data, mode is found by grouping or histogram estimation.

NumPy / SciPy Implementation

NumPy does not include a dedicated mode function. Instead, we use scipy.stats.mode() (from the SciPy library), which provides a robust and flexible implementation.


1. Function: scipy.stats.mode()

Syntax:

scipy.stats.mode(a, axis=0, nan_policy='propagate', keepdims=False)

2. Parameters

Parameter Description
a Input array or sequence
axis Axis along which to compute mode
nan_policy How to handle NaN values: ‘propagate’, ‘omit’, or ‘raise’
keepdims Keep reduced dimensions for broadcasting

3. Example 1: Basic Mode

import numpy as np
from scipy import stats

data = np.array([4, 2, 4, 3, 2, 4, 1])
mode_result = stats.mode(data, keepdims=True)

print("Mode:", mode_result.mode[0])
print("Frequency:", mode_result.count[0])

Output:

Mode: 4
Frequency: 3

The most frequent value is 4, appearing 3 times.


4. Example 2: Handling Multidimensional Data

matrix = np.array([[1, 2, 2],
                   [3, 3, 4],
                   [5, 5, 5]])

# Compute mode along columns
mode_col = stats.mode(matrix, axis=0, keepdims=True)
print("Column-wise mode:", mode_col.mode)

Output:

Column-wise mode: [[1 2 2]]

5. Example 3: Mode with Missing Values (NaN Handling)

data = np.array([1, 2, np.nan, 2, 3, 2, np.nan])
mode_result = stats.mode(data, nan_policy='omit', keepdims=True)

print("Mode (ignoring NaN):", mode_result.mode[0])

Output:

Mode (ignoring NaN): 2.0

nan_policy='omit' ensures missing data does not affect the result.


6. Example 4: Custom Frequency Check (Manual Count)

You can manually compute the mode using numpy.unique() if SciPy isn’t available:

data = np.array([1, 2, 2, 3, 3, 3, 4])
values, counts = np.unique(data, return_counts=True)
mode_value = values[np.argmax(counts)]

print("Mode:", mode_value)

Output:

Mode: 3

7. Example 5: Categorical Data Mode

data = np.array(['cat', 'dog', 'cat', 'bird', 'cat', 'dog'])
mode_result = stats.mode(data, keepdims=True)
print("Mode:", mode_result.mode[0])

Output:

Mode: cat

Works with strings or categorical values too.


8. Applications in Data Science

  • Market analysis: Most common purchase, brand, or category
  • Survey data: Most selected answer (like “Yes”, “No”, “Maybe”)
  • Image processing: Used in mode filters for noise reduction
  • Feature engineering: For imputing missing categorical values

Summary

Concept Definition Suitable For Python Function Handles NaN Sensitive to Outliers
Mode Most frequent value Categorical / discrete data scipy.stats.mode() Yes (nan_policy='omit') ❌ No

Comparison (Mean vs Median vs Mode)

Feature Mean Median Mode
Definition Average Middle value Most frequent value
Data Type Numeric Numeric / Ordinal Numeric / Categorical
Outlier Sensitivity High Low None
When to Use Normal data Skewed data Categorical data