Photo by Katie Rainbow π³οΈβπ on Unsplash
Mean-Variance-Standard Deviation Calculator: freeCodeCamp
A certification project provided by freeCodeCamp
Description
This is a simple project assigned by freeCodeCamp as a part of their certification course. The in-depth requirement of the project can be found on their website, but the general idea of it is to be able to calculate and output mean, variance, standard deviation, max, min and sum values of the provided argument.
Project Detail
The folder structure is maintained by freeCodeCamp themselves.
Mean-Variance-Standard Deviation Calculator/
β
βββ mean_var_std.py
βββ main.py
βββ test_module.py
The project is initialized through main.py
. Everything is laid out for us, we just need to execute this file. One interesting thing to note is that it automatically runs the test_module.py
file for us.
# main.py
# This entrypoint file to be used in development. Start by reading README.md
import mean_var_std
from unittest import main
print(mean_var_std.calculate([0,1,2,3,4,5,6,7,8]))
# Run unit tests automatically
main(module='test_module', exit=False)
# test_module.py
import unittest
import mean_var_std
# the test case
class UnitTests(unittest.TestCase):
def test_calculate(self):
actual = mean_var_std.calculate([2,6,2,8,4,0,1,5,7])
expected = {'mean': [[3.6666666666666665, 5.0, 3.0], [3.3333333333333335, 4.0, 4.333333333333333], 3.888888888888889], 'variance': [[9.555555555555557, 0.6666666666666666, 8.666666666666666], [3.555555555555556, 10.666666666666666, 6.222222222222221], 6.987654320987654], 'standard deviation': [[3.091206165165235, 0.816496580927726, 2.943920288775949], [1.8856180831641267, 3.265986323710904, 2.494438257849294], 2.6434171674156266], 'max': [[8, 6, 7], [6, 8, 7], 8], 'min': [[1, 4, 0], [2, 0, 1], 0], 'sum': [[11, 15, 9], [10, 12, 13], 35]}
self.assertAlmostEqual(actual, expected, "Expected different output when calling 'calculate()' with '[2,6,2,8,4,0,1,5,7]'")
def test_calculate2(self):
actual = mean_var_std.calculate([9,1,5,3,3,3,2,9,0])
expected = {'mean': [[4.666666666666667, 4.333333333333333, 2.6666666666666665], [5.0, 3.0, 3.6666666666666665], 3.888888888888889], 'variance': [[9.555555555555555, 11.555555555555557, 4.222222222222222], [10.666666666666666, 0.0, 14.888888888888891], 9.209876543209875], 'standard deviation': [[3.0912061651652345, 3.39934634239519, 2.0548046676563256], [3.265986323710904, 0.0, 3.8586123009300755], 3.0347778408328137], 'max': [[9, 9, 5], [9, 3, 9], 9], 'min': [[2, 1, 0], [1, 3, 0], 0], 'sum': [[14, 13, 8], [15, 9, 11], 35]}
self.assertAlmostEqual(actual, expected, "Expected different output when calling 'calculate()' with '[9,1,5,3,3,3,2,9,0]'")
def test_calculate_with_few_digits(self):
self.assertRaisesRegex(ValueError, "List must contain nine numbers.", mean_var_std.calculate, [2,6,2,8,4,0,1,])
if __name__ == "__main__":
unittest.main()
The main task for us lies in mean_var_std.py
. We need to write code that passes every test while executing the main
file. And so this is the code that I've written:
# mean_var_std.py
import numpy as np
def calculate(list):
if(len(list) != 9):
raise ValueError('List must contain nine numbers.')
calculations = {
'mean': [],
'variance': [],
'standard deviation': [],
'max': [],
'min': [],
'sum': []
}
# create a 3x3 matrix
npArr = np.array(list)
mat = np.array([list[0:3], list[3:6], list[6:]])
# mean calculations
meanAxisCol = mat.mean(axis=0).tolist()
meanAxisRow = mat.mean(axis=1).tolist()
meanFlat = npArr.mean()
calculations['mean'] = [meanAxisCol,meanAxisRow, meanFlat]
# variance calculations
varAxisCol = mat.var(axis=0).tolist()
varAxisRow = mat.var(axis=1).tolist()
varFlat = npArr.var()
calculations['variance'] = [varAxisCol, varAxisRow, varFlat]
# standard deviation calculations
stdAxisCol = mat.std(axis=0).tolist()
stdAxisRow = mat.std(axis=1).tolist()
stdFlat = npArr.std()
calculations['standard deviation'] = [stdAxisCol, stdAxisRow, stdFlat]
# max calculations
maxAxisCol = mat.max(axis=0).tolist()
maxAxisRow = mat.max(axis=1).tolist()
maxFlat = npArr.max()
calculations['max'] = [maxAxisCol, maxAxisRow, maxFlat]
# min calculations
minAxisCol = mat.min(axis=0).tolist()
minAxisRow = mat.min(axis=1).tolist()
minFlat = npArr.min()
calculations['min'] = [minAxisCol, minAxisRow, minFlat]
# sum calculations
sumAxisCol = mat.sum(axis=0).tolist()
sumAxisRow = mat.sum(axis=1).tolist()
sumFlat = npArr.sum()
calculations['sum'] = [sumAxisCol, sumAxisRow, sumFlat]
return calculations
So starting off, there is a check at the beginning of the function. As the requirement stated, that we need to throw a ValueError
when the length of the list
is not equal to 9
. Then, I declared a dictionary whose structure was pre-defined in the requirement as well.
The coding part begins now π€£. There might definitely be a way to improve the code to be "efficient πββοΈ" but this is what I could come up with at that moment. So, the list is broken down and a 3x3
matrix is created. I did it as such:
mat = np.array([list[0:3], list[3:6], list[6:]])
I did not use the help of ChatGpt at all and tried to limit my self from googling too much while coding this π€£ so please bear with me. So now that I asked ChatGpt, I could have just done .reshape(3, 3)
π. So, if you are reading this use the .reshape()
method π.
Moving on, the processes are fairly simple and repetitive in a way. I just calculate values based on each axis i.e., axis=0
and axis=1
. If you don't know the concepts of axes, you should definitely google it as it is fairly easy to get confused. And lastly, calculate the value for the original list itself.
After calculation of each value i.e., mean, variance, standard deviation, max, min and sum, I update the dictionary with respective values.
Insights
Working on this project, I got to practice some fundamental concepts of working with numpy. Stuffs like axis and matrices and methods provided by the numpy array which makes life easierπ€£ (which I learned as I'm writing this article).
Conclusion
And yea, that is it. Fairly simple I suppose π. If you want to take on this project I will provide the link here as well so you don't have to scroll up. Try not to google too much or take help from ChatGpt and try to work with what you remember. See ya later π.