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]
方法 #2:使用 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]