So I believe that I am 90% done with this challenge but I have ran into 2 problems:
- My formatting for the calculation output is off. For some reason the output include the word array in my calculation output for each calculation that I do. I have tried to research this problem to the best of my ability so I am not sure if I am describing it correctly because I didn’t get any usual information.
- My input validation isn’t properly working. I believe I have it set up according to the documentation that I found to meet the requirements of the challenge but I don’t know why it isn’t properly working.
If anyone can teach me what I am doing wrong that would be greatly appreciated.
import numpy as np
def calculate(list):
#0.5 Input validation for list
while True:
try:
#1. Transform list into a 3x3 matrix
arr = np.asarray(list)
matx = np.reshape(arr, (3,3))
break
except ValueError(len(list) < 9):
print("List must contain nine numbers.")
#2. Create Dict and compute stats for matrix
calculations = {
'Mean': [[np.mean(matx,axis=0)], [np.mean(matx, axis=1)], np.mean(matx)],
'Variance': [[np.var(matx,axis=0)], [np.var(matx, axis=1)], np.var(matx)],
'Std Deviation': [[np.std(matx,axis=0)], [np.std(matx, axis=1)], np.std(matx)],
'Max': [[np.max(matx,axis=0)], [np.max(matx, axis=1)], np.max(matx)],
'Min': [[np.min(matx,axis=0)], [np.min(matx, axis=1)], np.min(matx)],
'Sum': [[np.sum(matx,axis=0)], [np.sum(matx, axis=1)], np.sum(matx)]
}
print(calculations)
return calculations
Console Output Log
python main.py
{'Mean': [[array([3., 4., 5.])], [array([1., 4., 7.])], 4.0], 'Variance': [[array([6., 6., 6.])], [array([0.66666667, 0.66666667, 0.66666667])], 6.666666666666667], 'Std Deviation': [[array([2.44948974, 2.44948974, 2.44948974])], [array([0.81649658, 0.81649658, 0.81649658])], 2.581988897471611], 'Max': [[array([6, 7, 8])], [array([2, 5, 8])], 8], 'Min': [[array([0, 1, 2])], [array([0, 3, 6])], 0], 'Sum': [[array([ 9, 12, 15])], [array([ 3, 12, 21])], 36]}
{'Mean': [[array([3., 4., 5.])], [array([1., 4., 7.])], 4.0], 'Variance': [[array([6., 6., 6.])], [array([0.66666667, 0.66666667, 0.66666667])], 6.666666666666667], 'Std Deviation': [[array([2.44948974, 2.44948974, 2.44948974])], [array([0.81649658, 0.81649658, 0.81649658])], 2.581988897471611], 'Max': [[array([6, 7, 8])], [array([2, 5, 8])], 8], 'Min': [[array([0, 1, 2])], [array([0, 3, 6])], 0], 'Sum': [[array([ 9, 12, 15])], [array([ 3, 12, 21])], 36]}
{'Mean': [[array([3.66666667, 5. , 3. ])], [array([3.33333333, 4. , 4.33333333])], 3.888888888888889], 'Variance': [[array([9.55555556, 0.66666667, 8.66666667])], [array([ 3.55555556, 10.66666667, 6.22222222])], 6.987654320987654], 'Std Deviation': [[array([3.09120617, 0.81649658, 2.94392029])], [array([1.88561808, 3.26598632, 2.49443826])], 2.6434171674156266], 'Max': [[array([8, 6, 7])], [array([6, 8, 7])], 8], 'Min': [[array([1, 4, 0])], [array([2, 0, 1])], 0], 'Sum': [[array([11, 15, 9])], [array([10, 12, 13])], 35]}
E{'Mean': [[array([4.66666667, 4.33333333, 2.66666667])], [array([5. , 3. , 3.66666667])], 3.888888888888889], 'Variance': [[array([ 9.55555556, 11.55555556, 4.22222222])], [array([10.66666667, 0. , 14.88888889])], 9.209876543209875], 'Std Deviation': [[array([3.09120617, 3.39934634, 2.05480467])], [array([3.26598632, 0. , 3.8586123 ])], 3.0347778408328137], 'Max': [[array([9, 9, 5])], [array([9, 3, 9])], 9], 'Min': [[array([2, 1, 0])], [array([1, 3, 0])], 0], 'Sum': [[array([14, 13, 8])], [array([15, 9, 11])], 35]}
EE
======================================================================
ERROR: test_calculate (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-mean-variance-standard-deviation-calculator/test_module.py", line 10, in test_calculate
self.assertAlmostEqual(actual, expected, "Expected different output when calling 'calculate()' with '[2,6,2,8,4,0,1,5,7]'")
File "/usr/lib/python3.8/unittest/case.py", line 943, in assertAlmostEqual
diff = abs(first - second)
TypeError: unsupported operand type(s) for -: 'dict' and 'dict'
======================================================================
ERROR: test_calculate2 (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-mean-variance-standard-deviation-calculator/test_module.py", line 15, in test_calculate2
self.assertAlmostEqual(actual, expected, "Expected different output when calling 'calculate()' with '[9,1,5,3,3,3,2,9,0]'")
File "/usr/lib/python3.8/unittest/case.py", line 943, in assertAlmostEqual
diff = abs(first - second)
TypeError: unsupported operand type(s) for -: 'dict' and 'dict'
======================================================================
ERROR: test_calculate_with_few_digits (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-mean-variance-standard-deviation-calculator/mean_var_std.py", line 16, in calculate
matx = np.reshape(arr, (3,3))
File "<__array_function__ internals>", line 5, in reshape
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/numpy/core/fromnumeric.py", line 299, in reshape
return _wrapfunc(a, 'reshape', newshape, order=order)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/numpy/core/fromnumeric.py", line 58, in _wrapfunc
return bound(*args, **kwds)
ValueError: cannot reshape array of size 7 into shape (3,3)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/runner/boilerplate-mean-variance-standard-deviation-calculator/test_module.py", line 18, in test_calculate_with_few_digits
self.assertRaisesRegex(ValueError, "List must contain nine numbers.", mean_var_std.calculate, [2,6,2,8,4,0,1,])
File "/usr/lib/python3.8/unittest/case.py", line 1357, in assertRaisesRegex
return context.handle('assertRaisesRegex', args, kwargs)
File "/usr/lib/python3.8/unittest/case.py", line 202, in handle
callable_obj(*args, **kwargs)
File "/home/runner/boilerplate-mean-variance-standard-deviation-calculator/mean_var_std.py", line 19, in calculate
except ValueError(len(list) < 9):
TypeError: catching classes that do not inherit from BaseException is not allowed
----------------------------------------------------------------------
Ran 3 tests in 0.008s
FAILED (errors=3)
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36
.
Challenge: Mean-Variance-Standard Deviation Calculator
Link to the challenge: