# Data Analysis with Python Projects - Mean-Variance-Standard Deviation Calculator

Tell us what’s happening:
Describe your issue in detail here.

I wrote my code, yet when I want to test my code with the given test cases they all give errors. But the solution is correct since when I take the given test cases and paste them to main, they give expected results too.

``````import numpy as np

def calculate(list):
try:
matrix = np.array(list).reshape(3, 3)
calculations = {}

# First we handle mean
axis1 = np.mean(matrix, axis=0)
axis2 = np.mean(matrix, axis=1)
flattened = np.mean([axis1, axis2])
calculations['mean'] = [axis1.tolist(), axis2.tolist(), flattened.tolist()]

#We do variation now
axis1 = np.var(matrix, axis=0)
axis2 = np.var(matrix, axis=1)
flattened = np.var([axis1, axis2])
calculations['variance'] = [axis1.tolist(), axis2.tolist(), flattened.tolist()]

#We do standart deviation
axis1 = np.std(matrix, axis=0)
axis2 = np.std(matrix, axis=1)
flattened = np.std([axis1, axis2])
calculations['standard deviation'] = [axis1.tolist(), axis2.tolist(), flattened.tolist()]

#We do max element
axis1 = np.max(matrix, axis=0)
axis2 = np.max(matrix, axis=1)
flattened = np.max([axis1, axis2])
calculations['max'] = [axis1.tolist(), axis2.tolist(), flattened.tolist()]

#We do min element
axis1 = np.min(matrix, axis=0)
axis2 = np.min(matrix, axis=1)
flattened = np.min([axis1, axis2])
calculations['min'] = [axis1.tolist(), axis2.tolist(), flattened.tolist()]

#We do sum element
axis1 = np.sum(matrix, axis=0)
axis2 = np.sum(matrix, axis=1)
flattened = np.sum([axis1, axis2])
calculations['sum'] = [axis1.tolist(), axis2.tolist(), flattened.tolist()]
except ValueError:
print("List must contain nine numbers.")
return calculations
``````

Given test cases

``````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()
``````

Challenge: Data Analysis with Python Projects - Mean-Variance-Standard Deviation Calculator

Looks good so far. Here is my out put with your code:

``````calculate([0,1,2,3,4,5,6,7,8])
``````
``````{'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],
7.111111111111112],
'standard deviation': [[2.449489742783178,
2.449489742783178,
2.449489742783178],
[0.816496580927726, 0.816496580927726, 0.816496580927726],
0.8164965809277259],
'max': [[6, 7, 8], [2, 5, 8], 8],
'min': [[0, 1, 2], [0, 3, 6], 0],
'sum': [[9, 12, 15], [3, 12, 21], 72]}
``````

Everything is correct except the last number 72, the example says it should be 36. Maybe check that out?

``````For example, calculate([0,1,2,3,4,5,6,7,8]) should return:

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

Thanks for the heads up on that, I haven’t noticed that until you said it. Well, I solved that issue with the code line below:

``````flattened = np.sum(matrix)
``````

Since they want the sum of the whole array, unlike the rest. However, I am still getting errors when I try to run the tests they gave (freeCodeCamp gives the test cases) and I get some errors when I run them

``````======================================================================
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 "/nix/store/hd4cc9rh83j291r5539hkf6qd8lgiikb-python3-3.10.8/lib/python3.10/unittest/case.py", line 876, 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 "/nix/store/hd4cc9rh83j291r5539hkf6qd8lgiikb-python3-3.10.8/lib/python3.10/unittest/case.py", line 876, 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/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 "/nix/store/hd4cc9rh83j291r5539hkf6qd8lgiikb-python3-3.10.8/lib/python3.10/unittest/case.py", line 1291, in assertRaisesRegex
return context.handle('assertRaisesRegex', args, kwargs)
File "/nix/store/hd4cc9rh83j291r5539hkf6qd8lgiikb-python3-3.10.8/lib/python3.10/unittest/case.py", line 201, in handle
callable_obj(*args, **kwargs)
File "/home/runner/boilerplate-mean-variance-standard-deviation-calculator/mean_var_std.py", line 45, in calculate
return calculations
UnboundLocalError: local variable 'calculations' referenced before assignment

----------------------------------------------------------------------
Ran 3 tests in 0.009s

FAILED (errors=3)
``````

Any help why these occur?

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (`</>`) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

1 Like

I noticed the issue and the issue is indeed about the provided test cases (for me, at least). See, I was getting the same result as the test cases. But since the fractional parts didn’t have the same amount of numbers, I got these errors. But since the code was working and when I received the same results as the test cases wanted (I tried each one individually as a result), I submitted my solution.

Please don’t submit a solution that fails the test suite. That sounds like you (mis)used global variables accidentally.

Submitting a solution you know does not pass may result in losing your certificate.

Can you post a new link to your code? I wouldn’t be able to provide any more insight than these errors.

That said, it’s unlikely to be a problem with the test. You do need to code for the tests in many cases, not just code for the problem description.

https://stackoverflow.com/questions/13361510/typeerror-unsupported-operand-types-for-dict-items-and-dict-items

``````Ran 3 tests in 0.001s

OK
``````

Tests seem to pass ok

1 Like

Thanks for the help man, appreciated

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.