Mean median mode in Python without libraries
Mean, median and mode are fundamental topics of statistics. You can easily calculate them in Python, with and without the use of external libraries.
These three are the main measures of central tendency. The central trend allows us to know the "normal" or "average" values of a data set. If you’re just starting out with data science, this is the tutorial for you. The Mean, Median, and Mode are techniques that are often used in Machine Learning, so it is important to understand the concept behind them.
This article shows you how to use Python to calculate mean, median, and mode without using external libraries.
Mean in Python
Mean: The mean is the average of all numbers and is sometimes called the arithmetic mean. This code computes the mean or average of a list of numbers:
# Python program to print
# mean of elements
# list of elements to calculate mean
n_num = [1, 2, 3, 4, 5]
n = len(n_num)
get_sum = sum(n_num)
mean = get_sum / n
print("Mean / Average is: " + str(mean))
Output:
Mean / Average is: 3.0
We define a list of numbers and calculate the length of the list. We then use the sum () function to get the sum of all the elements in a list. Finally, we divide the total by the number of items in the list and output the result to get the mean of a list.
Median in Python
Median: The median is the middle number in a group of numbers. The median is the mean value of a ranked dataset. It is used - again - to provide a “typical” value for a given population.
In programming, we can define the median as the value that separates a sequence into two parts - the lower half and the upper half. To calculate the median, we first need to sort the dataset. We could do this with sorting algorithms or using the built-in sorted() function. The second step is to determine whether the length of the dataset is odd or even.
This code calculates the median of a list of numbers:
# Python program to print
# median of elements
# list of elements to calculate median
n_num = [1, 2, 3, 4, 5]
n = len(n_num)
n_num.sort()
if n % 2 == 0:
median1 = n_num[n//2]
median2 = n_num[n//2 - 1]
median = (median1 + median2)/2
else:
median = n_num[n//2]
print("Median is: " + str(median))
Output:
Median is: 3
We define a list of numbers and calculate the length of the list. To find a median, we first sort the list in ascending order using the sort () function.
Now let’s check if the number is even or odd by checking their remainders. If the number is even, we find 2 intermediate elements in a list and we get their average to print it. But if the number is odd, we find the central element in a list and print it.
Mean median mode in Python
Mode in Python
Mode: The mode is the number that occurs most often within a set of numbers. Mode is the most common value in the dataset. We can think of this as a “popular” school group that can represent the standard for all students.
An example of a mode would be daily sales at a tech store. The mode of this dataset will be the best selling product for a given day.
This code calculates the mode of a list containing numbers:
# Python program to print
# mode of elements
from collections import Counter
# list of elements to calculate mode
n_num = [1, 2, 3, 4, 5, 5]
n = len(n_num)
data = Counter(n_num)
get_mode = dict(data)
mode = [k for k, v in get_mode.items() if v == max(list(data.values()))]
if len(mode) == n:
get_mode = "No mode found"
else:
get_mode = "Mode is / are: " + ’, ’.join(map(str, mode))
print(get_mode)
Output:
Mode is / are: 5
We will import Counter from the collection library which is a built-in module in Python 2 and 3. This module will help us to count duplicate items in a list.
We define a list of numbers and calculate the length of the list. So we call Counter (a dict subclass) which helps count hashable objects and then convert it to a dict object. We then start a list with a For loop to compare all dict values (number of elements) with the maximum of all dict values (number of multiple elements) and return all elements equal to the maximum number. If the returned items are equal to the total number of items in a list, we display "No modes", otherwise we display the returned modes.
# The list for which you need to find
# the Mode
y= [11, 8, 8, 3, 4, 4, 5, 6, 6, 6, 7, 8]
# First you sort it
# You will get numbers arranged from 3 to
# 11 in asc order
y.sort()
# Now open an empty list.
# What you are going to do is to count
# the occurrence of each number and append
# (or to add your findings to) L1
L1=[]
# You can iterate through the sorted list
# of numbers in y,
# counting the occurrence of each number,
# using the following code
i = 0
while i < len(y) :
L1.append(y.count(y[i]))
i += 1
# your L1 will be [1, 2, 2, 1, 3, 3, 3, 1, 3, 3, 3, 1],
# the occurrences for each number in sorted y
# now you can create a custom dictionary d1 for k : V
# where k = your values in sorted y
# and v = the occurrences of each value in y
# the Code is as follows
d1 = dict(zip(y, L1))
# your d1 will be {3: 1, 4: 2, 5: 1, 6: 3, 7: 1, 8: 3, 11: 1}
# now what you need to do is to filter
# the k values with the highest v values.
# do this with the following code
d2={k for (k,v) in d1.items() if v == max(L1) }
print("Mode(s) is/are :" + str(d2))
Output:
Mode(s) is/are :{8, 6}
How to find mode in Python without inbuilt function
We have successfully calculated the mean, median, and dataset mode, but you might be thinking, "Will I use these algorithms every time I want to get the mean, median, and dataset mode?"
Answer: you can, but of course you won’t. It was just to show you how the algorithm works behind the scenes when it detects any of them.
For any project, this can be achieved by simply importing the built-in statistics library in Python 3 and using the mean (), median () and mode () built-in functions. In addition, there are other external libraries that can help you achieve the same results in just 1 line of code, since the code is pre-written in these libraries.