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

Tell us what’s happening:
I solved the challenge quite fast but even though when I ask the result lists of their class and they show me that they are Python lists, the print shows the ‘array’ in front of the list as if it is a numpy.array
Like this naturally it always fails the test_module.

import numpy as np

def calculate(list):
print(list)
x =

``````try:
x = np.array(list).reshape(3, 3)
except ValueError :
raise ValueError("List must contain nine numbers.")
print(x)

calculations = dict([
('mean', [x.mean(0), x.mean(1), x.mean()].__class__),
('variance', [x.var(0), x.var(1), x.var()]),
('standard deviation', [x.std(0), x.std(1), x.std()]),
('max', [x.max(0), x.max(1), x.max()]),
('min', [x.min(0), x.min(1), x.min()]),
('sum', [x.sum(0), x.sum(1), x.sum()])
])
print(calculations)
return calculations
``````

calculate([9,1,5,3,3,3,2,9,0])

** resulting output **  python test_module.py [9, 1, 5, 3, 3, 3, 2, 9, 0] [[9 1 5] [3 3 3] [2 9 0]] {‘mean’: <class ‘list’>, ‘variance’: [array([ 9.55555556, 11.55555556, 4.22222222]), array([10.66666667, 0. , 14.88888889]), 9.209876543209875], ‘standard deviation’: [array([3.09120617, 3.39934634, 2.05480467]), array([3.26598632, 0. , 3.8586123 ]), 3.0347778408328137], ‘max’: [array([9, 9, 5]), array([9, 3, 9]), 9], ‘min’: [array([2, 1, 0]), array([1, 3, 0]), 0], ‘sum’: [array([14, 13, 8]), array([15, 9, 11]), 35]} [2, 6, 2, 8, 4, 0, 1, 5, 7] [[2 6 2] [8 4 0] [1 5 7]] {‘mean’: <class ‘list’>, ‘variance’: [array([9.55555556, 0.66666667, 8.66666667]), array([ 3.55555556, 10.66666667, 6.22222222]), 6.987654320987654], ‘standard deviation’: [array([3.09120617, 0.81649658, 2.94392029]), array([1.88561808, 3.26598632, 2.49443826]), 2.6434171674156266], ‘max’: [array([8, 6, 7]), array([6, 8, 7]), 8], ‘min’: [array([1, 4, 0]), array([2, 0, 1]), 0], ‘sum’: [array([11, 15, 9]), array([10, 12, 13]), 35]} E[9, 1, 5, 3, 3, 3, 2, 9, 0] [[9 1 5] [3 3 3] [2 9 0]] {‘mean’: <class ‘list’>, ‘variance’: [array([ 9.55555556, 11.55555556, 4.22222222]), array([10.66666667, 0. , 14.88888889]), 9.209876543209875], ‘standard deviation’: [array([3.09120617, 3.39934634, 2.05480467]), array([3.26598632, 0. , 3.8586123 ]), 3.0347778408328137], ‘max’: [array([9, 9, 5]), array([9, 3, 9]), 9], ‘min’: [array([2, 1, 0]), array([1, 3, 0]), 0], ‘sum’: [array([14, 13, 8]), array([15, 9, 11]), 35]} E[2, 6, 2, 8, 4, 0, 1] .

ERROR: test_calculate (main.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/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’

====================================================================== ERROR: test_calculate2 (main.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/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’

Ran 3 tests in 0.019s

FAILED (errors=2)
exit status 1

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

I am trying to learn Python which is why I am doing this and I try to understand why:
[x.mean(0), x.mean(1), x.mean()].class ==> results in <class ‘list’>
but prints something like:
‘variance’: [array([ 9.55555556, 11.55555556, 4.22222222]), array([10.66666667, 0. , 14.88888889]), 9.209876543209875] ==> means it is a numpy.array ?!?

If I try to convert it like :
[x.mean(0), x.mean(1), x.mean()].tolist() ==> it gives me an error as this method is not implemented for lists.

I would appreciate very much if someone could help me with my confusion.

Thanks,
Stephan

Would you mind posting your code and output within triple backticks, or use the code icon here, so it is formatted correctly? Will make it easier to read and troubleshoot. Thanks!

``````triple backticks look like this
``````

If it reports that it’s a list, it’s a list and is enclosed in `[square brackets]`

An array is also enclosed in `[square brackets]` so it looks similar, but it’s functionally different.

This shows the difference and similarity very well: https://www.geeksforgeeks.org/basics-of-numpy-arrays/

Thanks, and no I wouldn’t mind. Is there a possibility to edit my post? I don’t seem to find it.

Hi,
Thanks a lot for the reference but that explains what I learned already in my search for a solution. When you print a Numpy array it always shows the “array(…)” denominator which is why I am confused because if I tell the array to show me it’s class […].class it says that it’s a list but then it shouldn’t print with the array(…) around it, no?

Just to try the backticks as this editor auto-deleted my underscores. I wanted to write:

``````[ ... ].__class__
``````

OK, interestingly enough if I take all the list creation out of the dictionary it works even though I don’t like the resulting coding style.

``````import numpy as np

def calculate(list):
x = np.array(list)

if(len(list) != 9):
raise ValueError("List must contain nine numbers.")

# this is a shit copy/paste coding style but solves my problem
a_rows = [x[[0,1,2]].mean(), x[[3,4,5]].mean(), x[[6,7,8]].mean()]
b_rows = [x[[0,1,2]].var(), x[[3,4,5]].var(), x[[6,7,8]].var()]
c_rows = [x[[0,1,2]].std(), x[[3,4,5]].std(), x[[6,7,8]].std()]
d_rows = [x[[0,1,2]].max(), x[[3,4,5]].max(), x[[6,7,8]].max()]
e_rows = [x[[0,1,2]].min(), x[[3,4,5]].min(), x[[6,7,8]].min()]
f_rows = [x[[0,1,2]].sum(), x[[3,4,5]].sum(), x[[6,7,8]].sum()]

a_cols = [x[[0,3,6]].mean(), x[[1,4,7]].mean(), x[[2,5,8]].mean()]
b_cols = [x[[0,3,6]].var(), x[[1,4,7]].var(), x[[2,5,8]].var()]
c_cols = [x[[0,3,6]].std(), x[[1,4,7]].std(), x[[2,5,8]].std()]
d_cols = [x[[0,3,6]].max(), x[[1,4,7]].max(), x[[2,5,8]].max()]
e_cols = [x[[0,3,6]].min(), x[[1,4,7]].min(), x[[2,5,8]].min()]
f_cols = [x[[0,3,6]].sum(), x[[1,4,7]].sum(), x[[2,5,8]].sum()]

calculations = {
'mean': [a_cols, a_rows, x.mean()],
'variance': [b_cols, b_rows, x.var()],
'standard deviation': [c_cols, c_rows, x.std()],
'max': [d_cols, d_rows, x.max()],
'min': [e_cols, e_rows, x.min()],
'sum': [f_cols, f_rows, x.sum()]
}
return calculations
``````

This is creating a list.

It’s interesting, not 100% why it’s this way but if you put an array in a list like this:

``````print([x])
``````

It will print:

``````[array([[9, 1, 5],
[3, 3, 3],
[2, 9, 0]])]
``````

But if you just print the array:

``````  print(x)
``````
``````[[9 1 5]
[3 3 3]
[2 9 0]]
``````

When you had this code, this is an array `x.mean(0)`:

``````>>>'mean', [type(x.mean(0), x.mean(1), x.mean()]
'mean': [numpy.ndarray, array([5.        , 3.        , 3.66666667]),  3.888888888888889],
``````

You are printing a list of arrays, which is why it said “array” in front.

This is just a number: `x[[0,1,2]].mean()` so now, you are printing a list of numbers.

``````>>>print(type(x[[0,1,2]].mean()))
<class 'numpy.float64'>
``````

In my first attempt I used a numpy matrix of 3x3 and then used x.mean(0) to get the average of all columns which actually returns a numpy.array. In my second attempt I took the list as flat numpy.array and indexed the values I wanted to average so I got numbers back and then made a list out of them.

What I should have done is:

``````    calculations = { # this creates a dictonary
'mean': [x.mean(0).tolist(), x.mean(1).tolist(), x.mean()],
'variance': [x.var(0).tolist(), x.var(1).tolist(), x.var()],
'standard deviation': [x.std(0).tolist(), x.std(1).tolist(), x.std()],
'max': [x.max(0).tolist(), x.max(1).tolist(), x.max()],
'min': [x.min(0).tolist(), x.min(1).tolist(), x.min()],
'sum': [x.sum(0).tolist(), x.sum(1).tolist(), x.sum()]
}
``````

as I understand now. I have tried to solve the problem on the wrong level.

1 Like

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