Sunday, 9 March 2025

Mode to represent central tendency

Mode represents the most frequent value in the dataset.

Example 1: For the data set {1, 2, 4, 5, 2, 3, 5,  5}, mode is 5, as it occurred 3 times in the dataset.

 

Example 2: For the data set {1, 2, 3, 4, 5} mode is undefined, as no value appears more than other in the dataset.

 

Unimodal mode: Mode is exactly one value.

 

For example, in the dataset {1, 2, 3, 4, 5, 1}. The Number 1 is repeated twice, and it is unimodal mode.

 

Bimodal mode: Mode is exactly two values.

 

For example, in the dataset {1, 2, 3, 4, 5, 1, 3}. The numbers 1 and 3 are repeated twice, and the numbers {2, 3} together represent bimodal mode.

 

Multimodal mode: Mode is more than two values.

 

For example, in the dataset {1, 2, 3, 4, 5, 1, 3, 2}. The numbers 1, 2 and 3 are repeated twice, and the numbers {1, 2, 3} together represent Multimodal mode

 

Advantages of mode

  1. Easy to implement
  2. Not affected by outliers
  3. Used for data sets that are not normally distributed.

 

Examples in real world

  1. Favourite game of students in a class
  2. Busiest time in a restaurant
  3. Most popular movies in 2023
  4. Most popular names in India

 

Find the below working application.

 

mode_calculator.py 

def calculate_mode(data_points):
    # Create a dictionary to count the occurrences of each value
    data_points_count = {}

    # Initialize the maximum count
    max_count = 0

    # Initialize the mode list
    mode_list = []

    for value in data_points:
        if value in data_points_count:
            data_points_count[value] += 1
        else:
            data_points_count[value] = 1

        # Update the maximum count
        if data_points_count[value] > max_count:
            max_count = data_points_count[value]

    # Find all values with the maximum count (modes)
    for key, value in data_points_count.items():
        if value == max_count:
            mode_list.append(key)

    return mode_list


data = [1, 2, 2, 3, 4, 4, 4, 5, 2]

# Calculate the mode(s)
modes = calculate_mode(data)

print("Modes:", modes)

 

Output

Modes: [2, 4]

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment