计算两个给定 NumPy 数组的平均值
求 NumPy 数组的平均值与求给定数字的平均值非常相似。我们只需要得到相应数组元素的总和,然后将该总和除以数组的总数。
让我们看一个例子:
示例 1:计算两个给定 NumPy 一维数组的平均值
Python3
# import library
import numpy as np
# create a numpy 1d-arrays
arr1 = np.array([3, 4])
arr2 = np.array([1, 0])
# find average of NumPy arrays
avg = (arr1 + arr2) / 2
print("Average of NumPy arrays:\n",
avg)
Python3
# import library
import numpy as np
# create a numpy 2d-arrays
arr1 = np.array([[3, 4], [8, 2]])
arr2 = np.array([[1, 0], [6, 6]])
# find average of NumPy arrays
avg = (arr1 + arr2) / 2
print("Average of NumPy arrays:\n",
avg)
输出:
Average of NumPy arrays:
[2. 2.]
示例 2:计算两个给定 NumPy 二维数组的平均值
Python3
# import library
import numpy as np
# create a numpy 2d-arrays
arr1 = np.array([[3, 4], [8, 2]])
arr2 = np.array([[1, 0], [6, 6]])
# find average of NumPy arrays
avg = (arr1 + arr2) / 2
print("Average of NumPy arrays:\n",
avg)
输出:
Average of NumPy arrays:
[[2. 2.]
[7. 4.]]