Add Mean-Variance-Standard Deviation Calculator

This commit is contained in:
CherryKitten 2022-12-03 11:08:40 +01:00
parent 99c34a5e62
commit df1fb04d72
Signed by: sammy
GPG key ID: 0B696A86A853E955
6 changed files with 77 additions and 0 deletions

View file

@ -0,0 +1,3 @@
# Mean-Variance-Standard Deviation Calculator
This is the boilerplate for the Mean-Variance-Standard Deviation Calculator project. Instructions for building your project can be found at https://www.freecodecamp.org/learn/data-analysis-with-python/data-analysis-with-python-projects/mean-variance-standard-deviation-calculator

View file

@ -0,0 +1,8 @@
# 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)

View file

@ -0,0 +1,16 @@
import numpy as np
def calculate(list):
if len(list) != 9:
raise ValueError("List must contain nine numbers.")
list = np.array(list).reshape(3, 3)
result = {
"mean": [np.mean(list, axis=0).tolist(), np.mean(list, axis=1).tolist(), np.mean(list)],
"variance": [np.var(list, axis=0).tolist(), np.var(list, axis=1).tolist(), np.var(list)],
"standard deviation": [np.std(list, axis=0).tolist(), np.std(list, axis=1).tolist(), np.std(list)],
"max": [np.max(list, axis=0).tolist(), np.max(list, axis=1).tolist(), np.max(list)],
"min": [np.min(list, axis=0).tolist(), np.min(list, axis=1).tolist(), np.min(list)],
"sum": [np.sum(list, axis=0).tolist(), np.sum(list, axis=1).tolist(), np.sum(list)]
}
return result

View file

@ -0,0 +1,14 @@
[[package]]
category = "main"
description = "NumPy is the fundamental package for array computing with Python."
name = "numpy"
optional = false
python-versions = ">=3.5"
version = "1.18.5"
[metadata]
content-hash = "362a4cdb0a177f527c73204355c2f4424c04716ca4bf001d1ab0c27db017feef"
python-versions = "^3.8"
[metadata.hashes]
numpy = ["0172304e7d8d40e9e49553901903dc5f5a49a703363ed756796f5808a06fc233", "34e96e9dae65c4839bd80012023aadd6ee2ccb73ce7fdf3074c62f301e63120b", "3676abe3d621fc467c4c1469ee11e395c82b2d6b5463a9454e37fe9da07cd0d7", "3dd6823d3e04b5f223e3e265b4a1eae15f104f4366edd409e5a5e413a98f911f", "4064f53d4cce69e9ac613256dc2162e56f20a4e2d2086b1956dd2fcf77b7fac5", "4674f7d27a6c1c52a4d1aa5f0881f1eff840d2206989bae6acb1c7668c02ebfb", "7d42ab8cedd175b5ebcb39b5208b25ba104842489ed59fbb29356f671ac93583", "965df25449305092b23d5145b9bdaeb0149b6e41a77a7d728b1644b3c99277c1", "9c9d6531bc1886454f44aa8f809268bc481295cf9740827254f53c30104f074a", "a78e438db8ec26d5d9d0e584b27ef25c7afa5a182d1bf4d05e313d2d6d515271", "a7acefddf994af1aeba05bbbafe4ba983a187079f125146dc5859e6d817df824", "a87f59508c2b7ceb8631c20630118cc546f1f815e034193dc72390db038a5cb3", "ac792b385d81151bae2a5a8adb2b88261ceb4976dbfaaad9ce3a200e036753dc", "b03b2c0badeb606d1232e5f78852c102c0a7989d3a534b3129e7856a52f3d161", "b39321f1a74d1f9183bf1638a745b4fd6fe80efbb1f6b32b932a588b4bc7695f", "cae14a01a159b1ed91a324722d746523ec757357260c6804d11d6147a9e53e3f", "cd49930af1d1e49a812d987c2620ee63965b619257bd76eaaa95870ca08837cf", "e15b382603c58f24265c9c931c9a45eebf44fe2e6b4eaedbb0d025ab3255228b", "e91d31b34fc7c2c8f756b4e902f901f856ae53a93399368d9a0dc7be17ed2ca0", "ef627986941b5edd1ed74ba89ca43196ed197f1a206a3f18cc9faf2fb84fd675", "f718a7949d1c4f622ff548c572e0c03440b49b9531ff00e4ed5738b459f011e8"]

View file

@ -0,0 +1,15 @@
[tool.poetry]
name = "fcc-mean-var-std"
version = "0.1.0"
description = ""
authors = ["Your Name <you@example.com>"]
[tool.poetry.dependencies]
python = "^3.8"
numpy = "^1.18"
[tool.poetry.dev-dependencies]
[build-system]
requires = ["poetry>=0.12"]
build-backend = "poetry.masonry.api"

View file

@ -0,0 +1,21 @@
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()