Python查找Numpy数组列表的平均值
给定一个Numpy数组的列表,任务是找到每个numpy数组的平均值。让我们看看有几种方法可以完成这个任务。
方法#1:使用np.mean()
# Python code to find mean of every numpy array in list
# Importing module
import numpy as np
# List Initialization
Input = [np.array([1, 2, 3]),
np.array([4, 5, 6]),
np.array([7, 8, 9])]
# Output list initialization
Output = []
# using np.mean()
for i in range(len(Input)):
Output.append(np.mean(Input[i]))
# Printing output
print(Output)
输出:
[2.0, 5.0, 8.0]
方法二:使用np.average()。
# Python code to find mean of
# every numpy array in list
# Importing module
import numpy as np
# List Initialization
Input = [np.array([11, 12, 13]),
np.array([14, 15, 16]),
np.array([17, 18, 19])]
# Output list initialization
Output = []
# using np.mean()
for i in range(len(Input)):
Output.append(np.average(Input[i]))
# Printing output
print(Output)
输出:
[12.0, 15.0, 18.0]