📌  相关文章
📜  对 Numpy 数组的每 N 个元素求平均值

📅  最后修改于: 2022-05-13 01:54:19.170000             🧑  作者: Mango

对 Numpy 数组的每 N 个元素求平均值

在本文中,我们将学习如何找到 NumPy 数组中每 n 个元素的平均值。为了完成我们的任务,我们将使用 NumPy 模块提供的一些内置方法,如下所示:

  • numpy.average()计算平均值,即所有数字的总和除以元素数
  • numpy.reshape()在不改变原始数据的情况下一次调整 n 个元素的数组
  • numpy.mean() 将平均值计算为平均值只不过是元素的总和除以元素的数量

示例 1:一维数组的平均值

Python3
import numpy as np
  
# converting list to numpy array
givenArray = np.array([6, 5, 4, 3, 2, 1, 9,
                       8, 7, 12, 11, 10, 15, 
                       14, 13])
  
# here we took 3 as our input
n = 3
  
# calculates the average
avgResult = np.average(givenArray.reshape(-1, n), axis=1)
  
print("Given array:")
print(givenArray)
  
print("Averaging over every ", n, " elements of a numpy array:")
print(avgResult)


Python3
import numpy as np
  
# converting list to numpy array
givenArray = np.array([[60, 50, 40], [30, 20, 10], [90, 80,70],
                       [120, 110, 100], [150, 140, 130]])
  
# here we took 5 as our input
n = 5
  
# calculates the average
avgResult = np.average(givenArray.reshape(-1, n), axis=1)
  
print("Given array:")
print(givenArray, "\n")
  
print("Dimensions of given array:", givenArray.shape, "\n")
  
print("Averaging over every ", n, " elements of a numpy array:")
print(avgResult)


Python3
import numpy as np
  
# converting list to numpy array
givenArray = np.array([[60, 50, 40], [30, 20, 10], [90, 80, 70],
                       [120, 110, 100], [150, 140, 130]])
  
# here we will calculate average
# over every 5 elements
n = 5
  
# calculates the average
avgResult = np.average(givenArray.reshape(-1, n), axis=0)
  
print("Given array:")
print(givenArray, "\n")
  
print("Dimensions of given array:", givenArray.shape, "\n")
  
print("Averaging over every ", n, " elements of a numpy array:")
print(avgResult)


Python3
import numpy as np
# converting list to numpy array
givenArray = np.array([[60, 50, 40], [30, 20, 10], [90, 80,70],
                       [120, 110, 100], [150, 140, 130]])
  
# here we will calculate average over
# every 5 elements
n = 5
  
# calculates the average
avgResult1 = givenArray.mean(axis=0)
  
print("Given array:")
print(givenArray, "\n")
  
print("Dimensions of given array:", givenArray.shape, "\n")
  
print("Averaging over every ", n, " elements of a numpy array:")
print(avgResult1)


输出:



注意: N 应该是一维数组大小的整数倍。

示例 2:一维数组的平均值(行方式)

这里我们采用了一个维度数组 (5,3),即它有 5 行和 3 列。由于axis=1,它将以n 为一组重新调整元素的形状,然后使用axis=1 计算行方向的平均值。

蟒蛇3

import numpy as np
  
# converting list to numpy array
givenArray = np.array([[60, 50, 40], [30, 20, 10], [90, 80,70],
                       [120, 110, 100], [150, 140, 130]])
  
# here we took 5 as our input
n = 5
  
# calculates the average
avgResult = np.average(givenArray.reshape(-1, n), axis=1)
  
print("Given array:")
print(givenArray, "\n")
  
print("Dimensions of given array:", givenArray.shape, "\n")
  
print("Averaging over every ", n, " elements of a numpy array:")
print(avgResult)

输出:

示例 3:一维数组的平均值(按列)

请记住,我们只需要给axis=1,然后它就可以从第0 个索引开始按行对元素进行分组。现在,如果我们将轴值更改为 0,那么在按 n 组重新整形后,它将按列执行平均操作,如下所示,这不会给我们想要的结果。如果我们想按列计算平均值是最好的。



蟒蛇3

import numpy as np
  
# converting list to numpy array
givenArray = np.array([[60, 50, 40], [30, 20, 10], [90, 80, 70],
                       [120, 110, 100], [150, 140, 130]])
  
# here we will calculate average
# over every 5 elements
n = 5
  
# calculates the average
avgResult = np.average(givenArray.reshape(-1, n), axis=0)
  
print("Given array:")
print(givenArray, "\n")
  
print("Dimensions of given array:", givenArray.shape, "\n")
  
print("Averaging over every ", n, " elements of a numpy array:")
print(avgResult)

重塑二维数组后,如下所示:

然后执行平均列明智我们得到答案。

输出:

示例 4:一维数组的平均值(按列计算,无需整形)

请注意,这里采用 axis=0 我们不能对每个 n 元素逐行执行平均。它只会分别计算每列的平均值。下面的代码将计算每个列元素的平均值。

蟒蛇3

import numpy as np
# converting list to numpy array
givenArray = np.array([[60, 50, 40], [30, 20, 10], [90, 80,70],
                       [120, 110, 100], [150, 140, 130]])
  
# here we will calculate average over
# every 5 elements
n = 5
  
# calculates the average
avgResult1 = givenArray.mean(axis=0)
  
print("Given array:")
print(givenArray, "\n")
  
print("Dimensions of given array:", givenArray.shape, "\n")
  
print("Averaging over every ", n, " elements of a numpy array:")
print(avgResult1)

输出: