字典输出中包含数组。

huangapple go评论45阅读模式
英文:

Dictionary output has array inside

问题

1- You can combine the functions into a single function by using a loop to calculate the mean, variance, standard deviation, max, min, and sum for both axis 0 and axis 1. Here's a modified version of your code:

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 function
def calculate_statistics(axis):
    mean = arr.mean(axis=axis)
    variance = arr.var(axis=axis)
    std_dev = arr.std(axis=axis)
    max_val = arr.max(axis=axis)
    min_val = arr.min(axis=axis)
    total_sum = arr.sum(axis=axis)
    return mean, variance, std_dev, max_val, min_val, total_sum

result["mean"], result["variance"], result["standard deviation"], result["max"], result["min"], result["sum"] = calculate_statistics(0), calculate_statistics(1)

for k, v in result.items():
    print(k, v)

2- The "array" word is coming from the fact that the values inside the dictionary are NumPy arrays. To convert them to regular Python lists, you can use the tolist() method. Here's how you can modify the code to do that:

result["mean"], result["variance"], result["standard deviation"], result["max"], result["min"], result["sum"] = [stat.tolist() for stat in calculate_statistics(0)], [stat.tolist() for stat in calculate_statistics(1)]

This modification will convert the NumPy arrays to regular Python lists before storing them in the result dictionary.

英文:

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

答案1

得分: 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()

打印结果:

{'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]}

关于为什么会打印出 "array",是因为,例如,np.mean(arr, axis=1) 返回一个NumPy数组。

英文:

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.

huangapple
  • 本文由 发表于 2023年2月16日 16:28:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75469562.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定