In this article, we will find the mean, median, and mode without using any external libraries in Python for Data Science.
- Mean: It is the average of all numbers and is also referr to as arithmetic mean. The code below will calculate the mean of a list that contains numbers.
- Median: It is the number that is exactly in the middle in a group of numbers. The below code finds the median from a list of numbers.
- Mode: The number which occurs the most number of times in a given set of numbers is known as the mode. Code for calculation of mode for a list of numbers is given below.
In the example, we have import the Counter from collections for calculating the duplicate element in the list.
We have define a list of numbers and length of the list. Now, we have then call the counter, which helps in counting the hashable objects and then convert them into dict objects. We have then initialize a for loop to compare all the dict values to max of all dict values and then it will return the element having the maximum count.
We have the above programs for understanding the working, and it should not be there for coding in Python generally. We generally import the statistics library and use the in-built functions mean(), median() and mode() for calculation.