0

I am trying on of the online tutorials to have a dictionary of nine numbers and create another dictionary with statistics, below is the code with the input data, and the result as well

import numpy as np
a = [0, 1, 2, 3, 4, 5, 6, 7, 8]

arr = np.array(a).reshape(3, 3).astype(int)

result = {
    "mean": [],
    "variance": [], 
    "standard deviation": [], 
    "max": [], 
    "min": [], 
    "sum": []
    }

# Creating a function1
def calculate1(a):
    calculate1 = arr.mean(axis = a)
    return(calculate1)
result["mean"].append(calculate1(0))
result["mean"].append(calculate1(1))
result["mean"].append(calculate1(None))

# Creating a function2
def calculate2(a):
    calculate2 = arr.var(axis = a)
    return(calculate2)
result["variance"].append(calculate2(0))
result["variance"].append(calculate2(1))
result["variance"].append(calculate2(None))

# Creating a function3
def calculate3(a):
    calculate3 = arr.std(axis = a)
    return(calculate3)
result["standard deviation"].append(calculate3(0))
result["standard deviation"].append(calculate3(1))
result["standard deviation"].append(calculate3(None))

# Creating a function4
def calculate4(a):
    calculate4 = arr.max(axis = a)
    return(calculate4)
result["max"].append(calculate4(0))
result["max"].append(calculate4(1))
result["max"].append(calculate4(None))

# Creating a function5
def calculate5(a):
    calculate5 = arr.min(axis = a)
    return(calculate5)
result["min"].append(calculate5(0))
result["min"].append(calculate5(1))
result["min"].append(calculate5(None))

# Creating a function6
def calculate6(a):
    calculate6 = arr.sum(axis = a)
    return(calculate6)
result["sum"].append(calculate6(0))
result["sum"].append(calculate6(1))
result["sum"].append(calculate6(None))
for k, v in result.items():
    print(k, v) 

And here is the result

mean [array([3., 4., 5.]), array([1., 4., 7.]), 4.0]
variance [array([6., 6., 6.]), array([0.66666667, 0.66666667, 0.66666667]), 6.666666666666667]
standard deviation [array([2.44948974, 2.44948974, 2.44948974]), array([0.81649658, 0.81649658, 0.81649658]), 2.581988897471611]
max [array([6, 7, 8]), array([2, 5, 8]), 8]
min [array([0, 1, 2]), array([0, 3, 6]), 0]
sum [array([ 9, 12, 15]), array([ 3, 12, 21]), 36]

I have two questions here:

1- Is there a way that I can combine or minimize the number of functions to one or something like that. Please note that I (have to) use the function.

2- The output is correct (in values), however I am not sure why the word (array) is printing as well, and when I check the type of the values inside the dictionary, it shows that they are <class 'list'>, so where this array word is coming from? I tried tolist value and plenty of online suggestions but nothing worked

Any help or suggestion is highly appreciated

Ali
  • 37
  • 1
  • 5

1 Answers1

0

You can store your functions inside a dict and then iterate over it:

from pprint import pprint

import numpy as np


def main():
    arr = np.random.rand(3, 3)

    functions = {
        "mean": lambda axis: arr.mean(axis=axis),
        "var": lambda axis: arr.var(axis=axis),
        "std": lambda axis: arr.std(axis=axis),
        "max": lambda axis: arr.max(axis=axis),
        "min": lambda axis: arr.min(axis=axis),
        "sum": lambda axis: arr.sum(axis=axis),
    }

    axes = (0, 1, None)

    result = {}
    for funcname, func in functions.items():
        result[funcname] = [func(axis).tolist() for axis in axes]

    # Alternatively:
    result = {
        funcname: [func(axis).tolist() for axis in axes]
        for funcname, func in functions.items()
    }


    pprint(result)


if __name__ == "__main__":
    main()

Prints:

{'max': [[0.33149413492721314, 0.9252576833729358, 0.9616249059176883],
         [0.37580580905770067, 0.9616249059176883, 0.9252576833729358],
         0.9616249059176883],
 'mean': [[0.23391570323037428, 0.4063894010374775, 0.6764668740080081],
          [0.20197437573445387, 0.4652236940918113, 0.6495739084495947],
          0.43892399275862],
 'min': [[0.0958037701384552, 0.13431354800720574, 0.37580580905770067],
         [0.0958037701384552, 0.15959697173229104, 0.33149413492721314],
         0.0958037701384552],
 'std': [[0.10039824223253171, 0.3670404461719236, 0.23941075106262735],
         [0.1239187264736742, 0.35412651334119355, 0.24424967197333333],
         0.3170854368356986],
 'sum': [[0.7017471096911229, 1.2191682031124325, 2.029400622024024],
         [0.6059231272033616, 1.395671082275434, 1.948721725348784],
         3.95031593482758],
 'var': [[0.010079807043382115, 0.13471868912608476, 0.057317507724371324],
         [0.015355850770857285, 0.12540558745119054, 0.05965790225908093],
         0.10054317425328584]}

As for why there is "array" printed, it is because, e.g., np.mean(arr, axis=1) returns a numpy array.

paime
  • 2,901
  • 1
  • 6
  • 17
  • Thanks, very short code, can you please explain in simple English what the last if does? I just could not figure it out. also, regarding the (array), if I use print(type(result['mean'])), it shows me , so how is it an array? and finally how to get rid of it and make it a list please? – Ali Feb 16 '23 at 10:24
  • Look [What does `if __name__ == "__main__":` do?](https://stackoverflow.com/questions/419163/what-does-if-name-main-do). (Essentially it's good practice). – paime Feb 16 '23 at 11:59
  • 1
    `result['mean']` is a `list`, but it contains `np.ndarray`s (check `type(result["mean"][0])`). You can convert them to list just after computing them: `result[funcname] = [func(axis).tolist() for axis in axes]`. – paime Feb 16 '23 at 12:01
  • 1
    Also added an even shorter for loop using dict comprehension – paime Feb 16 '23 at 12:08