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

Tell us what’s happening:
Hi, everyone,
I am taking this general course about Python.
The result of my code shows array[], array[]...,
though I am seeking a way not to show such words “array” but the result inside the bracelet directly.
Thank you all for reading.

Your code so far

        import numpy as np

def calculate(list):
  if len(list)<9:
   print("List must contain nine numbers.")
  else:
   a=np.reshape(list,(3,3))
  b=(np.mean(a,axis=0),np.mean(a,axis=1),np.mean(a))
  c=(np.var(a,axis=0),np.var(a,axis=1),np.var(a))
  d=(np.std(a,axis=0),np.std(a,axis=1),np.std(a))
  e=(np.max(a,axis=0),np.max(a,axis=1),np.max(a))
  f=(np.min(a,axis=0),np.min(a,axis=1),np.min(a))
  g=(np.sum(a,axis=0),np.sum(a,axis=1),np.sum(a))
  calculations={  'mean': [b],'variance': [c],'standard deviation': [d],'max': [e],'min': [f],'sum': [g] }
  return calculations
print(calculate([2,6,2,8,4,0,1,5,7]))

Your browser information:

Challenge: Mean-Variance-Standard Deviation Calculator

Link to the challenge:

1 Like

So, you get for example 'mean': [(array([3.66666667, 5. , 3. ]), array([3.33333333, 4. , 4.33333333]), 3.888888888888889)] , the outer square parenthesis is because you have written 'mean': [b], removing the square parenthesis you get instead 'mean': (array([3.66666667, 5. , 3. ]), array([3.33333333, 4. , 4.33333333]), 3.888888888888889) - the square parenthesis

the rest is because of what the value of b is
add print(b) and you get (array([3.66666667, 5. , 3. ]), array([3.33333333, 4. , 4.33333333]), 3.888888888888889)
if you don’t want that you need to change the value of b


I’ve edited your post 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 (’).

Thank you for the advice!!

you can convert array to list like this :

b = [(np.mean(a,axis=0)).tolist(),(np.mean(a,axis=1)).tolist(),np.mean(a)]
2 Likes

Hi, and thanks in advance for any help the community can offer.

Have written the following solution for this problem:

import numpy as np

def calculate(list):
  if len(list) != 9:
    raise ValueError('List must contain nine numbers.')
  else:
    a = list[:3]
    b = list[3:6]
    c = list[6:]
    m1 = np.array([a, b, c])
    calculations = {
      'mean ': (np.mean(m1, axis=0).tolist(), np.mean(m1, axis=1).tolist(), np.mean(list)),
      'variance ': (np.var(m1, axis=0).tolist(), np.var(m1, axis=1).tolist(), np.var(list)),
      'standard deviation ': (np.std(m1, axis=0).tolist(), np.std(m1, axis=1).tolist(), np.std(list)),
      'max ': (np.max(m1, axis=0).tolist(), np.max(m1, axis=1).tolist(), np.max(list)),
      'min ': (np.min(m1, axis=0).tolist(), np.min(m1, axis=1).tolist(), np.min(list)),
      'sum ': (np.sum(m1, axis=0).tolist(), np.sum(m1, axis=1).tolist(), np.sum(list))
    }
    return(calculations)

Which appears to do the job mostly, but I’m still getting the following 2 errors:

ERROR: test_calculate (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/ImperfectPepperyApplicationframework/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/ImperfectPepperyApplicationframework/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'

----------------------------------------------------------------------

Can anyone help me understand what I’m doing wrong here? Can’t quite figure it out.

Thanks,
Hal

After I was done with this, I kept having errors and I don’t know where the problem is

Hi,
Your calculations dictionary contains tuples () but required list []. Maybe this causes a type error.

2 Likes

I tried this and I still get error messages.