Mean-Variance-Standard Deviation Calculator ValueError

I am getting error ‘ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()’.

How do I fix this?

My code:

import numpy as np

def calculate(list):

  if len(list) < 9:
    raise ValueError("List must contain nine numbers.");
  
  else:
  
    arr = np.array(list).reshape(3,3)

    a = np.mean(arr, axis = 0)
    b = np.mean(arr, axis = 1)
    c = np.mean(arr)
    d = np.var(arr, axis = 0)
    e = np.var(arr, axis = 1)
    f = np.var(arr)
    g = np.std(arr, axis = 0)
    h = np.std(arr, axis = 1)
    i = np.std(arr)
    j = np.max(arr, axis = 0)
    k = np.max(arr, axis = 1)
    l = np.max(arr)
    m = np.min(arr, axis = 0)
    n = np.min(arr, axis = 1)
    o = np.min(arr)
    p = np.sum(arr, axis = 0)
    q = np.sum(arr, axis = 1)
    r = np.sum(arr)

    calculations = {
      'mean': [a,b,c],
      'variance': [d,e,f],
      'standard deviation': [g,h,i],
      'max': [j,k,l],
      'min': [m,n,o],
      'sum': [p,q,r]
    }

    return calculations

you need to have python lists, not numpy arrays


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 (’).

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