在这篇文章中,我们将看到如何在Python中计算移动平均值。移动平均是指总观测值集合中固定大小子集的一系列平均值。它也被称为滚动平均。
考虑n个观测值的集合,k是用于确定任何时间t的平均值的窗口的大小。然后,移动平均列表通过最初取当前窗口中存在的前k个观测值的平均值并将其存储在列表中来计算。现在,根据要确定的移动平均值的条件来扩展窗口,并且再次计算窗口中存在的元素的平均值并将其存储在列表中。这个过程一直持续到窗口到达集合的末尾。
例如:给定一个包含五个整数的列表 arr=[1,2,3,7,9],我们需要计算窗口大小为3的列表的移动平均值。我们将首先计算前3个元素的平均值,并将其存储为第一个移动平均值。然后窗口将向右移动一个位置,并再次计算窗口中存在的元素的平均值并存储在列表中。类似地,该过程将重复,直到窗口到达数组的最后一个元素。以下是对上述方法的说明:
下面是实现:
# Program to calculate moving average
arr = [1, 2, 3, 7, 9]
window_size = 3
i = 0
# Initialize an empty list to store moving averages
moving_averages = []
# Loop through the array to consider
# every window of size 3
while i < len(arr) - window_size + 1:
# Store elements from i to i+window_size
# in list to get the current window
window = arr[i : i + window_size]
# Calculate the average of current window
window_average = round(sum(window) / window_size, 2)
# Store the average of current
# window in moving average list
moving_averages.append(window_average)
# Shift window to right by one position
i += 1
print(moving_averages)
输出
[2.0, 4.0, 6.33]
简单移动平均
SMA(Simple Moving Average)的计算方法是取当前窗口中某个时间的k个(窗口大小)观测值的加权平均值。它用于分析趋势。
公式:
其中,
-
SMAj = 第j个窗口的简单移动平均值
-
k =窗口大小
-
ai =观测集的第i个元素
方法1:使用Numpy
Python的Numpy模块提供了一种简单的方法来计算观测数组的简单移动平均值。它提供了一个名为numpy.sum()的方法,该方法返回给定数组的元素之和。移动平均值可以通过找到窗口中存在的元素的总和并将其除以窗口大小来计算。
# Program to calculate moving average using numpy
import numpy as np
arr = [1, 2, 3, 7, 9]
window_size = 3
i = 0
# Initialize an empty list to store moving averages
moving_averages = []
# Loop through the array t o
#consider every window of size 3
while i < len(arr) - window_size + 1:
# Calculate the average of current window
window_average = round(np.sum(arr[
i:i+window_size]) / window_size, 2)
# Store the average of current
# window in moving average list
moving_averages.append(window_average)
# Shift window to right by one position
i += 1
print(moving_averages)
输出
[2.0, 4.0, 6.33]
方法2:使用Pandas
Python的Pandas模块提供了一种简单的方法来计算一系列观测值的简单移动平均值。它提供了一个名为pandas.Series.rolling(window_size)的方法,该方法返回指定大小的滚动窗口。窗口的平均值可以通过在上面获得的窗口对象上使用pandas.Series.mean()函数来计算。pandas.Series.rolling(window_size)将返回一些空序列,因为它至少需要k个(窗口大小)元素才能滚动。
# Python program to calculate
# simple moving averages using pandas
import pandas as pd
arr = [1, 2, 3, 7, 9]
window_size = 3
# Convert array of integers to pandas series
numbers_series = pd.Series(arr)
# Get the window of series
# of observations of specified window size
windows = numbers_series.rolling(window_size)
# Create a series of moving
# averages of each window
moving_averages = windows.mean()
# Convert pandas series back to list
moving_averages_list = moving_averages.tolist()
# Remove null entries from the list
final_list = moving_averages_list[window_size - 1:]
print(final_list)
输出
[2.0, 4.0, 6.33]
累积移动平均
CMA(Cumulative Moving Average)的计算方法是取计算时所有观测值的加权平均值。用于时间序列分析。
公式:
其中:
- CMAt = 时间t的累积移动平均值
- kt = 截至时间t的观测次数
- ai = 观测集的第i个元素
方法1:使用Numpy
Python的Numpy模块提供了一种简单的方法来计算观测数组的累积移动平均值。它提供了一个名为numpy.cumsum()的方法,该方法返回给定数组的元素的累积和的数组。移动平均值可以通过将元素的累积和除以窗口大小来计算。
# Program to calculate cumulative moving average
# using numpy
import numpy as np
arr = [1, 2, 3, 7, 9]
i = 1
# Initialize an empty list to store cumulative moving
# averages
moving_averages = []
# Store cumulative sums of array in cum_sum array
cum_sum = np.cumsum(arr);
# Loop through the array elements
while i <= len(arr):
# Calculate the cumulative average by dividing
# cumulative sum by number of elements till
# that position
window_average = round(cum_sum[i-1] / i, 2)
# Store the cumulative average of
# current window in moving average list
moving_averages.append(window_average)
# Shift window to right by one position
i += 1
print(moving_averages)
输出
[1.0, 1.5, 2.0, 3.25, 4.4]
方法2:使用Pandas
Python的Pandas模块提供了一种简单的方法来计算一系列观测值的累积移动平均值。它提供了一个名为pandas.Series.expanding()的方法,该方法返回一个窗口,该窗口覆盖了截至时间t的所有观察结果。窗口的平均值可以通过使用pandas.Series.mean()函数在上面获得的窗口对象上计算。
# Python program to calculate
# cumulative moving averages using pandas
import pandas as pd
arr = [1, 2, 3, 7, 9]
window_size = 3
# Convert array of integers to pandas series
numbers_series = pd.Series(arr)
# Get the window of series of
# observations till the current time
windows = numbers_series.expanding()
# Create a series of moving averages of each window
moving_averages = windows.mean()
# Convert pandas series back to list
moving_averages_list = moving_averages.tolist()
print(moving_averages_list)
输出
[1.0, 1.5, 2.0, 3.25, 4.4]
指数移动平均
EMA(Exponential Moving Average)是通过每次取观测值的加权平均值来计算的。观察值的权重随时间呈指数下降。它用于分析最近的变化。
公式:
其中:
- EMAt = 时间t的指数移动平均
- α = 观察权重随时间的降低程度
- at = 在时间t的观察
# Program to calculate exponential
# moving average using formula
import numpy as np
arr = [1, 2, 3, 7, 9]
x=0.5 # smoothening factor
i = 1
# Initialize an empty list to
# store exponential moving averages
moving_averages = []
# Insert first exponential average in the list
moving_averages.append(arr[0])
# Loop through the array elements
while i < len(arr):
# Calculate the exponential
# average by using the formula
window_average = round((x*arr[i])+
(1-x)*moving_averages[-1], 2)
# Store the cumulative average
# of current window in moving average list
moving_averages.append(window_average)
# Shift window to right by one position
i += 1
print(moving_averages)
输出
[1, 1.5, 2.25, 4.62, 6.81]
方法1:使用Pandas
Python的Pandas模块提供了一种简单的方法来计算一系列观测值的指数移动平均值。它提供了一种称为pandas.Series.ewm.mean()的方法,用于计算给定观测值的指数移动平均值。
pandas.Series.ewm()接受一个称为平滑因子的参数,即观察值的权重随时间减少的程度。平滑因子的值始终介于0和1之间。
# Python program to
# calculate exponential moving averages
import pandas as pd
arr = [1, 2, 3, 7, 9]
# Convert array of integers to pandas series
numbers_series = pd.Series(arr)
# Get the moving averages of series
# of observations till the current time
moving_averages = round(numbers_series.ewm(
alpha=0.5, adjust=False).mean(), 2)
# Convert pandas series back to list
moving_averages_list = moving_averages.tolist()
print(moving_averages_list)
输出
[1.0, 1.5, 2.25, 4.62, 6.81]
应用场景
- 时间序列分析:它用于平滑短期变化并突出长期观察,如趋势和周期。
- 金融分析:它用于股票市场的财务分析,如计算股票价格,回报和分析市场趋势。
- 环境工程:它用于分析环境条件,考虑各种因素,如污染物的浓度等。
- 计算机性能分析:它通过计算平均CPU利用率、平均进程队列长度等指标来分析计算机性能。