Measures of Central Tendency: Understanding the Middle of Your Data

Learn how to calculate and interpret these measures of central tendency to gain valuable insights.

Ahmad Mizan Nur Haq
Data And Beyond
4 min readNov 2, 2023

--

Introduction

In this article, we’ll explain the three most common measures of central tendency: the mean, median, and mode. We’ll also discuss how to calculate and interpret these measures, and provide examples of how they are used in the real world.

Mean

The mean, also known as the average, is the sum of all the values in a data set divided by the number of values in the data set.

To calculate the mean, simply add up all the values in the data set and divide by the number of values. For example, if the data set is {3, 4, 5, 6, 7}, the mean would be calculated as follows:

def calculate_mean(values):
total = sum(values)
mean = total / len(values)
return mean

# Example usage
data = [3,4,5,6,7] #try to experiment with it

mean_value = calculate_mean(data)
print(f"The mean of the data is: {mean_value}")

data = 3, 4 , 5, 6, 7

n = 5

3 + 4 + 5 + 6 + 7 / 5 = 5

Median

The median is the middle value in a data set that has been ordered from smallest to largest. If the data set has an even number of values, the median is the average of the two middle values.

To calculate the median, first order the data set from smallest to largest. If the data set has an odd number of values, the median is the middle value. If the data set has an even number of values, the median is the average of the two middle values. For example, if the data set is {3, 2, 2, 3, 4}.

if the data set is {3, 3, 5, 11, 13, 17, 7,15}.

def calculate_median(values):
sorted_values = sorted(values)
n = len(sorted_values)

if n % 2 == 1:
# For odd number of values, return the middle value
median = sorted_values[n // 2]
else:
# For even number of values, return the average of two middle values
median = (sorted_values[n // 2 - 1] + sorted_values[n // 2]) / 2

return median

# Example usage
data = [3, 3, 5, 11, 13, 17, 7,15]

median_value = calculate_median(data)
print(f"The median of the data is: {median_value}")

Mode

The mode is the most frequent value in a data set.

To calculate the mode, simply identify the most frequent value in the data set. For example, if the data set is {3, 3, 5, 11, 13, 17, 7,15}, the mode would be 3.

The mode is a good measure of central tendency when the data is categorical, such as hair color or eye color. However, the mode is not a very informative measure of central tendency when the data is numerical.

from collections import Counter

def calculate_mode(values):
value_counts = Counter(values)
max_count = max(value_counts.values())

mode = [val for val, count in value_counts.items() if count == max_count]

if len(mode) == len(values):
return "No mode"

return mode

# Example usage
data = [3, 3, 5, 11, 13, 17, 7,15]

mode_value = calculate_mode(data)

if mode_value == "No mode":
print(f"There is no mode in the data.")
else:
print(f"The mode(s) of the data is/are: {mode_value}")

Full code here : ⬇️

Conclusion

Measures of central tendency are a powerful tool for data analysis and visualization. By understanding how to calculate and interpret these measures, we can gain valuable insights into our data.

Hey 👋 Enjoying the content? If you find it valuable, why not subscribe and follow Me on Medium for more content!

🔔 Subscribe & Follow

☕️Buymeacoffee |📚Substack | GitHub | LinkedIn

By subscribing, you’ll get the latest updates, tips, and content delivered right to your inbox.

Thank you for your support! 🙌

In this post, I kindly want to see the effectiveness of Call to Action (CTA).

--

--