Python - Data Science - Mean/Median/Mode
What is a Mean?
A mean is the simple mathematical average of a set of two or more numbers.Eg:
The sum of the 57 Boys weight is 231.51 and hence the mean is 231.51/57 = 4.06
What is the Median?
The median is the middle number in a sorted, ascending or descending, list of numbers and can be more descriptive of that data set than the average.
For example, in a data set of {3, 13, 2, 34, 11, 26, 47}, the sorted order becomes {2, 3, 11, 13, 26, 34, 47}. The median is the number in the middle {2, 3, 11, 13, 26, 34, 47}, which in this instance is 13 since there are three numbers on either side.
For example, in a data set of {3, 13, 2, 34, 11, 17, 27, 47}, the sorted order becomes {2, 3, 11, 13, 17, 27, 34, 47}.
The median is the average of the two numbers in the middle which in this case is fifteen {2, 3, 11, 13, 17, 27, 34, 47} -> {(13 + 17) ÷ 2 = 15}.
What is a Mode?
The is the most frequent observation (or observations) in a sample.
We have the sample [4, 1, 2, 2, 3, 5] then its mode is 2 because 2 appears two times in the sample whereas the other elements only appear once.
The mode doesn't have to be unique. Some samples have more than one mode.
Say we have the sample [4, 1, 2, 2, 3, 5, 4].
This sample has two modes - 2 and 4 because they're the values that appear more often and both appear the same number of times.
Finding mean, median and mode using Python statistics module:
import statistics;
print(statistics.mean([4, 8, 6, 5, 3, 2, 8, 9, 2, 3, 4, 10]));
print(statistics.median([4, 8, 6, 5, 3, 2, 8, 9, 2, 3, 4, 10]))
print(statistics.mode([4, 8, 6, 5, 3, 2, 8, 9, 2, 3, 4, 10]));
Code:
# Calculating the Mean With Python
data=[4, 8, 6, 5, 3, 2, 8, 9, 2, 5];
print("Mean: ",sum(data)/len(data));
# Finding the Median of a Sample
data=[4, 8, 6, 5, 3, 2, 8, 9, 2, 3, 4, 10];
def even_median(data):
split = int(len(data)/2);
mean = [];
mean.append(data[split - 1]);
mean.append(data[split]);
print("Median:", mean);
if len(data) % 2 == 0:
even_median(data);
else:
split = int(len(data)/2);
print("Media:", data[split]);
# Finding mode using Counter module:
from collections import Counter
data=[4, 8, 6, 5, 3, 2, 8, 9, 2, 3, 4, 10];
c = Counter(data);
print(c);# Counter({4: 2, 8: 2, 3: 2, 2: 2, 6: 1, 5: 1, 9: 1, 10: 1})
print(c.most_common()); # [(4, 2), (8, 2), (3, 2), (2, 2), (6, 1), (5, 1), (9, 1), (10, 1)]
print(c.most_common(1)); # Since .most_common(1) returns a list with one tuple of the form (observation, count)
print(c.most_common(1)[0][0]); # Shows most recent value alone.
Comments
Post a Comment