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

Hello, I am having some trouble when I run my program from this project.
In test_module.py file this the error I get

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/xf54733x4chbawkh1qvy9i1i4mlscy1c-python3-3.10.11/lib/python3.10/unittest/case.py”, line 876, in assertAlmostEqual
diff = abs(first - second)
TypeError: unsupported operand type(s) for -: ‘dict’ and ‘dict’

However when I run the code from Visual Studio Code this is the result I get:

Result:
{‘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_value’: [[8, 6, 7], [6, 8, 7], 8], ‘min_value’: [[1, 4, 0], [2, 0, 1], 0], ‘sum’: [[11, 15, 9], [10, 12, 13], 35]}

and the expected result is:
{‘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]}

This is my code.

import numpy as np

def calculate(digits):

  try:
    # Converting the list into a 3 x 3 numpy array.
    numbers = np.array(digits).reshape(3, 3)
  
    # Calculate the mean of the newly formed array.
    m_axis1 = numbers.mean(axis=0) # Mean along axis1 - column.
    m_axis2 = numbers.mean(axis=1) # Mean along axis2 - row.
    m_flattened = numbers.mean()
  
    # Calculate the variance.
    v_axis1 = numbers.var(axis=0) # Variance along axis1 - column.
    v_axis2 = numbers.var(axis=1) # Variance along axis2 - column.
    v_flattened = numbers.var()
  
    # Calculate the standard deviation.
    std_axis1 = numbers.std(axis=0) # Standard deviation along axis1 - column.
    std_axis2 = numbers.std(axis=1) # Standard deviation along axis2 - row.
    std_flattened = numbers.std()
  
    # Find the max value.
    max_axis1 = numbers.max(axis=0) # Max value along axis1 - column.
    max_axis2 = numbers.max(axis=1) # Max value along axis2 - column.
    max_flattened = numbers.max()
  
    # Find the min value.
    min_axis1 = numbers.min(axis=0) # Min value along axis1 - column
    min_axis2 = numbers.min(axis=1) # Min value along axis2 - row
    min_flattened = numbers.min()
  
    # Calculate the sum.
    sum_axis1 = numbers.sum(axis=0) # Sum along axis1 - column.
    sum_axis2 = numbers.sum(axis=1) # Sum along axis2 - row.
    sum_flattened = numbers.sum()
  
    # Creating a dictionary that stores all the calculations
    calculations = {
        'mean': [list(m_axis1), list(m_axis2), m_flattened],
        'variance': [list(v_axis1), list(v_axis2), v_flattened],
        'standard deviation': [list(std_axis1), list(std_axis2), std_flattened],
        'max_value': [list(max_axis1), list(max_axis2), max_flattened],
        'min_value': [list(min_axis1), list(min_axis2), min_flattened],
        'sum': [list(sum_axis1), list(sum_axis2), sum_flattened],
    }
    # print(calculations)
    return calculations

  except ValueError:
    raise ValueError("List must contain nine numbers.")

Challenge Information:

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

‘max’: [[8, 6, 7], [6, 8, 7], 8],

You just need to check that these dictionary key values are the same as the example :+1: Other than this, nice work!

1 Like

Wow ! Thank you very much. I did think that the solution was that simple.

The error message is very impressive but all it really meant was that the dict did not match the expected answer in some way.

It’s also very easy to miss when you are trying to verify all those numbers.

1 Like