Error with first project of Data Analysis

my code: https://replit.com/@HarshilAv/boilerplate-mean-variance-standard-deviation-calculator-2#mean_var_std.py
I get an error that my average function call is undefined

Hi @harshilavlani , if Im not wrong, in the beginning of your code there is an identation error,


In def average(arr):
If you put in in another environment(like Jupyter), will automatically select the error.
hope this helps

2 Likes

Thanks, but now there is another error which I do not get at all
When I put compare the actual and expected output in a Jupyter Notebook, they are equal, but I am getting some kind of error on Replit.

Hard for us to comment without the error.

What kind of error, do you get?

I really am having trouble figuring that out, so here is a copy paste of my console.

python main.py
None
EEF

ERROR: test_calculate (test_module.UnitTests)

Traceback (most recent call last):
File “/home/runner/boilerplate-mean-variance-standard-deviation-calculator-2/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)
 python main.py
None
FEF

ERROR: test_calculate2 (test_module.UnitTests)

Traceback (most recent call last):
File “/home/runner/boilerplate-mean-variance-standard-deviation-calculator-2/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 -: ‘NoneType’ and ‘dict’

======================================================================
FAIL: test_calculate (test_module.UnitTests)

Traceback (most recent call last):
File “/home/runner/boilerplate-mean-variance-standard-deviation-calculator-2/test_module.py”, line 10, in test_calculate
self.assertEqual(actual, expected, “Expected different output when calling ‘calculate()’ with ‘[2,6,2,8,4,0,1,5,7]’”)
AssertionError: None != {‘mean’: [[3.6666666666666665, 5.0, 3.0],[485 chars] 35]} : Expected different output when calling ‘calculate()’ with ‘[2,6,2,8,4,0,1,5,7]’

======================================================================
FAIL: test_calculate_with_few_digits (test_module.UnitTests)

Traceback (most recent call last):
File “/home/runner/boilerplate-mean-variance-standard-deviation-calculator-2/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,])
AssertionError: ValueError not raised by calculate


Ran 3 tests in 0.002s

FAILED (failures=2, errors=1)

It appears to be related to the self.assertalmostequal call

It appears that your function is returning None


Ah, I see. You defined calculate inside of calculate but never actually do the calculations and return anything inside of the outer calculate function.

Thanks now I am getting an error that the Unittest module does not have the asseralmost equal function. here is my console now:

None
EEF

ERROR: test_calculate (test_module.UnitTests)

Traceback (most recent call last):
File “/home/runner/boilerplate-mean-variance-standard-deviation-calculator-2/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)
 python main.py
None
FEF

ERROR: test_calculate2 (test_module.UnitTests)

Traceback (most recent call last):
File “/home/runner/boilerplate-mean-variance-standard-deviation-calculator-2/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 -: ‘NoneType’ and ‘dict’

======================================================================
FAIL: test_calculate (test_module.UnitTests)

Traceback (most recent call last):
File "/home/runner/boilerplate-mean-variance-standard-deviation-c
 python main.py
{‘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’: [array([ 9, 12, 15]), array([ 3, 12, 21]), 36]}
 python main.py
3 by 3 array:
[[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], 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’: None}
3 by 3 array:
[[2 6 2]
[8 4 0]
[1 5 7]]
E3 by 3 array:
[[9 1 5]
[3 3 3]
 python main.py
3 by 3 array:
[[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], 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’: None}
3 by 3 array:
[[2 6 2]
[8 4 0]
[1 5 7]]
E3 by 3 array:
[[9 1 5]
[3 3 3]
[2 9 0]]
E.

ERROR: test_calculate (test_module.UnitTests)

Traceback (most recent call last):
File “/home/runner/boilerplate-mean-variance-standard-deviation-calculator-2/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-2/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’


Ran 3 tests in 0.012s

FAILED (errors=2)

Have you checked the output from main? Is everything actually a list?

yes I think so, but I am not sure

I see a lot of places where you did not cast to a list. Does every single place where you did not cast to a list return a list instead of NumPy array? NumPy function calls usually return NumPy data structures.

2 Likes

Thanks so much for all your help, I forgot to cast 2 np arrays to lists, and now that I did, It fixed the problem

2 Likes