Std mean calculation: TypeError: unsupported operand type(s) for -: 'dict' and 'dict'

I cant find why I am getting error. All the values are the same, but in different order (order shoudnt midn in dict, isnt it?)
Would be nice to understand what is wrong.
My code output

{
‘max’: [[8, 6, 7], [6, 8, 7], 8],
‘mean’: [[3.6666666666666665, 5.0, 3.0], [3.3333333333333335, 4.0 4.333333333333333],
3.888888888888889],
‘min’: [[1, 4, 0], [2, 0, 1], 0],
‘standard deviation’: [[3.091206165165235,
0.816496580927726,
2.943920288775949],
[1.8856180831641267, 3.265986323710904, 2.494438257849294],
2.6434171674156266],
‘sum’: [[11, 15, 9], [10, 12, 13], 35],
‘variance’: [[9.555555555555557, 0.6666666666666666, 8.666666666666666],
[3.5555555555555554, 10.666666666666666, 6.222222222222221],
6.987654320987655]
}

Example output:
{
‘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]

}

import numpy as np

def fix_answer(a, b, c):
    return [a.tolist(), b.tolist(), c.tolist()]

def calculate(data):
    if len(data) != 9:
        raise ValueError('List must contain nine numbers.')
    x = np.array(data)
    x = x.reshape(3,3)

    mu_x =  fix_answer(x.mean(axis=0), x.mean(axis=1), x.flatten().mean())
    std_x = fix_answer(x.std(axis=0), x.std(axis=1), x.flatten().std())
    var_x = fix_answer(x.std(axis=0)**2, x.std(axis=1)**2, x.flatten().std()**2)
    max_x = fix_answer(x.max(axis=0), x.max(axis=1), x.flatten().max())
    min_x = fix_answer(x.min(axis=0), x.min(axis=1), x.flatten().min())
    sum_x = fix_answer(x.sum(axis=0), x.sum(axis=1), x.flatten().sum())
    calculations = {}
    calculations['mean'] = list(mu_x)
    calculations['variance'] = var_x
    calculations['standard deviation'] = std_x
    calculations['max'] = max_x
    calculations['min'] = min_x
    calculations['sum'] = sum_x

    return calculations

Hm… yeah I got to much time on my hand xD
After doing a bunch of comparisons, I cannot tell you “why exactly” the error is happening, but I can tell you what to change: You have to use the x.var() method to calculate the variance.

My completly random assumption is, that **2 on the array causes some issues with the datatype, which results in an problem with the assertion-function that manifests at failing to properly compare the difference between the dictionairies.
I think this error usually happens if there is a spelling-mistake. Which you don’t have but similarly the assterion-function uses the “-” on the dictionairies which isn’t actually defined EXCEPT in this special case. But if you screw up the datatypes, it’s not longer the special case → hence giving this generic error.

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