在Python中计算移动平均值的方法

 更新时间:2024年10月15日 09:27:58   作者:python收藏家  
在这篇文章中,我们将看到如何在Python中计算移动平均值,移动平均是指总观测值集合中固定大小子集的一系列平均值,它也被称为滚动平均,文中通过代码示例讲解的非常详细,需要的朋友可以参考下

前言

在这篇文章中,我们将看到如何在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利用率、平均进程队列长度等指标来分析计算机性能。

到此这篇关于在Python中计算移动平均值的方法的文章就介绍到这了,更多相关Python计算移动平均值内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Python PyYAML库解析YAML文件使用详解

    Python PyYAML库解析YAML文件使用详解

    这篇文章主要为大家介绍了Python PyYAML库解析YAML文件使用详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-11-11
  • python pycharm中使用opencv时没有代码自动补全提示的解决方案

    python pycharm中使用opencv时没有代码自动补全提示的解决方案

    我们在使用pycharm的时候总是很喜欢其强大的代码提示功能,下面这篇文章主要给大家介绍了关于python pycharm中使用opencv时没有代码自动补全提示的解决方案,需要的朋友可以参考下
    2022-09-09
  • Windows下Python的Django框架环境部署及应用编写入门

    Windows下Python的Django框架环境部署及应用编写入门

    这篇文章主要介绍了Windows下Python的Django框架环境部署及程序编写入门,Django在Python的框架中算是一个重量级的MVC框架,本文将从程序部署开始讲到hellow world web应用的编写,需要的朋友可以参考下
    2016-03-03
  • Python sys.path详细介绍

    Python sys.path详细介绍

    这篇文章详细介绍了Python sys.path,有需要的朋友可以参考一下
    2013-10-10
  • python中单下划线_的常见用法总结

    python中单下划线_的常见用法总结

    这篇文章主要介绍了python中单下划线_的常见用法总结,其实很多(不是所有)关于下划线的使用都是一些约定俗成的惯例,而不是真正对python解释器有影响,感兴趣的朋友跟随脚本之家小编一起看看吧
    2018-07-07
  • python递归算法(无限递归,正常递归,阶乘)

    python递归算法(无限递归,正常递归,阶乘)

    本文主要介绍了python递归算法,包含无限递归,正常递归,阶乘等,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-02-02
  • python解决循环依赖的问题分析

    python解决循环依赖的问题分析

    在使用python开发过程中在引入其他模块时可能都经历过一个异常就是循环引用most likely due to a circular import,它的意思就是A引用了B,反过来B又引用了A,导致出现了循环引用异常,下面来介绍如何避免循环引用异常,感兴趣的朋友跟随小编一起看看吧
    2022-12-12
  • python实现csdn全部博文下载并转PDF

    python实现csdn全部博文下载并转PDF

    我们学习编程,在学习的时候,会有想把有用的知识点保存下来,我们可以把知识点的内容爬下来转变成pdf格式,方便我们拿手机可以闲时翻看,是很方便的,本文就介绍一下如何实现
    2021-06-06
  • node.js获取参数的常用方法(总结)

    node.js获取参数的常用方法(总结)

    下面小编就为大家带来一篇node.js获取参数的常用方法(总结)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-05-05
  • python优化测试稳定性的失败重试工具pytest-rerunfailures详解

    python优化测试稳定性的失败重试工具pytest-rerunfailures详解

    笔者在执行自动化测试用例时,会发现有时候用例失败并非代码问题,而是由于服务正在发版,导致请求失败,从而降低了自动化用例的稳定性,那该如何增加失败重试机制呢?带着问题我们一起探索
    2023-10-10

最新评论