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: