Mean-Variance-Standard Deviation Calculator - Help

Tell us what’s happening:
Hello everyone,
I’m trying to solve the first Data Analysis with Python PrPreformatted textoject/challenge.
The code works (at least visually), but the test_module.UnitTests gives me two errors :

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’

I saw this error in other questions, on google, but don’t seem to find the solution.
Any help is more than welcome.
Thank you

Your code so far
https://repl.it/@hamza531/boilerplate-mean-variance-standard-deviation-calculator#test_module.py

import numpy as np

def calculate(list):
if len (list) != 9:
raise ValueError(‘List must contain nine numbers.’)
else:
list_array = np.asarray(list)
reshaped_array = list_array.reshape(3,3)

mean_row = np.mean(reshaped_array, axis = 0).tolist()
mean_column = np.mean(reshaped_array, axis = 1).tolist()
mean = np.mean(reshaped_array).tolist()

variance_row = np.var(reshaped_array, axis = 0).tolist()
variance_column = np.var(reshaped_array, axis = 1).tolist()
variance = np.var(reshaped_array).tolist()

standard_deviation_row = np.std(reshaped_array, axis = 0).tolist()
standard_deviation_column = np.std(reshaped_array, axis = 1).tolist()
standard_deviation = np.std(reshaped_array).tolist()

max_row = np.max(reshaped_array, axis = 0).tolist()
max_column = np.max(reshaped_array, axis = 1).tolist()
max_ = np.max(reshaped_array).tolist()

min_row = np.min(reshaped_array, axis = 0).tolist()
min_column = np.min(reshaped_array, axis = 1).tolist()
min_ = np.min(reshaped_array).tolist()

sum_row = reshaped_array.sum(axis=0).tolist()
sum_column = reshaped_array.sum(axis = 1).tolist()
sum_ = reshaped_array.sum().tolist()

d = {'mean' : [[mean_row], [mean_column],[mean]], 
     'variance': [[variance_row],[variance_column],[variance]],
     'standard_deviation':[[standard_deviation_row], 
     [standard_deviation_column], [standard_deviation]],
     'max' : [[max_row],[max_column],[max_]],
     'min' : [[min_row],[min_column],[min_]],
     'sum' : [[sum_row],[sum_column],[sum_]]             
    }                                                                                                                                                      
  return d

a=(0,1,2,3,4,5,6,7,8)
calculate(a)

{‘mean’: [[[3.0, 4.0, 5.0]], [[1.0, 4.0, 7.0]], [4.0]],
‘variance’: [[[6.0, 6.0, 6.0]],
[[0.6666666666666666, 0.6666666666666666, 0.6666666666666666]],
[6.666666666666667]],
‘standard_deviation’: [[[2.449489742783178,
2.449489742783178,
2.449489742783178]],
[[0.816496580927726, 0.816496580927726, 0.816496580927726]],
[2.581988897471611]],
‘max’: [[[6, 7, 8]], [[2, 5, 8]], [8]],
‘min’: [[[0, 1, 2]], [[0, 3, 6]], [0]],
‘sum’: [[[9, 12, 15]], [[3, 12, 21]], [36]]
}

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.146 Safari/537.36.

Challenge: Mean-Variance-Standard Deviation Calculator

Link to the challenge:

Take a closer look at dict from the function and expected dict from the test.

{'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]]}

{'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]}

Thank you for your time Sanity.
I’ll check this and hope to find the correct answer.

Hi Sanity,

I saw that I had extra squared brackets in my dict and removed them, but it was not enough and I couldn’t find my other error(s).

So I decided to restart from scratch and I finally found the problem. I had an extra (again) “_” in my standard deviation key.

Thank you for your feedback.

PS : can someone send me a link where I can find more explanation about the “TypeError: unsupported operand type(s) for -: ‘dict’ and ‘dict’” ?

Thank you

Honestly that error can be misleading and confusing in here.

It comes from the way test is trying to check if the result is the same as expected. All is working fine when everything matches, because it’s able to determine that returned dict is even with the expected dict. Problems starts when that’s not the case, when test tries to determine the exact difference by subtracting one dict from another, what is not supported by this data type.

Thank you for your explanation .