找回密码
 立即注册

QQ登录

只需一步,快速开始

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

2024-11-5 01:35| 发布者: ae2942d9| 查看: 357| 评论: 0

摘要: 目录前言简朴移动均匀累积移动均匀指数移动均匀应用场景前言 在这篇文章中,我们将看到如安在Python中计算移动均匀值。移动均匀是指总观测值聚会集固定大小子集的一系列均匀值。它也被称为滚动均匀。 考虑n个观测值
目录

前言

在这篇文章中,我们将看到如安在Python中计算移动均匀值。移动均匀是指总观测值聚会集固定大小子集的一系列均匀值。它也被称为滚动均匀。

考虑n个观测值的聚集,k是用于确定任何时间t的均匀值的窗口的大小。然后,移动均匀列表通过最初取当前窗口中存在的前k个观测值的均匀值并将其存储在列表中来计算。现在,根据要确定的移动均匀值的条件来扩展窗口,并且再次计算窗口中存在的元素的均匀值并将其存储在列表中。这个过程不停连续到窗口到达聚集的末尾。

例如:给定一个包含五个整数的列表 arr=[1,2,3,7,9],我们须要计算窗口大小为3的列表的移动均匀值。我们将首先计算前3个元素的均匀值,并将其存储为第一个移动均匀值。然后窗口将向右移动一个位置,并再次计算窗口中存在的元素的均匀值并存储在列表中。类似地,该过程将重复,直到窗口到达数组的末了一个元素。以下是对上述方法的说明:

在这里插入图片描述

下面是实现:

[code]# 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) [/code]

输出

[code][2.0, 4.0, 6.33][/code]

简朴移动均匀

SMA(Simple Moving Average)的计算方法是取当前窗口中某个时间的k个(窗口大小)观测值的加权均匀值。它用于分析趋势。

公式:

在这里插入图片描述

此中,

  • SMAj = 第j个窗口的简朴移动均匀值
  • k =窗口大小
  • ai =观测集的第i个元素

方法1:利用Numpy

Python的Numpy模块提供了一种简朴的方法来计算观测数组的简朴移动均匀值。它提供了一个名为numpy.sum()的方法,该方法返回给定数组的元素之和。移动均匀值可以通过找到窗口中存在的元素的总和并将其除以窗口大小来计算。

[code]# 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) [/code]

输出

[code][2.0, 4.0, 6.33] [/code]

方法2:利用Pandas

Python的Pandas模块提供了一种简朴的方法来计算一系列观测值的简朴移动均匀值。它提供了一个名为pandas.Series.rolling(window_size)的方法,该方法返回指定大小的滚动窗口。窗口的均匀值可以通过在上面得到的窗口对象上利用pandas.Series.mean()函数来计算。pandas.Series.rolling(window_size)将返回一些空序列,因为它至少须要k个(窗口大小)元素才气滚动。

[code]# 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) [/code]

输出

[code][2.0, 4.0, 6.33][/code]

累积移动均匀

CMA(Cumulative Moving Average)的计算方法是取计算时所有观测值的加权均匀值。用于时间序列分析。

公式:

在这里插入图片描述

此中:

  • CMAt = 时间t的累积移动均匀值
  • kt = 截至时间t的观测次数
  • ai = 观测集的第i个元素

方法1:利用Numpy

Python的Numpy模块提供了一种简朴的方法来计算观测数组的累积移动均匀值。它提供了一个名为numpy.cumsum()的方法,该方法返回给定数组的元素的累积和的数组。移动均匀值可以通过将元素的累积和除以窗口大小来计算。

[code]# 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) [/code]

输出

[code][1.0, 1.5, 2.0, 3.25, 4.4] [/code]

方法2:利用Pandas

Python的Pandas模块提供了一种简朴的方法来计算一系列观测值的累积移动均匀值。它提供了一个名为pandas.Series.expanding()的方法,该方法返回一个窗口,该窗口覆盖了截至时间t的所有观察结果。窗口的均匀值可以通过利用pandas.Series.mean()函数在上面得到的窗口对象上计算。

[code]# 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) [/code]

输出

[code][1.0, 1.5, 2.0, 3.25, 4.4][/code]

指数移动均匀

EMA(Exponential Moving Average)是通过每次取观测值的加权均匀值来计算的。观察值的权重随时间呈指数降落。它用于分析最近的厘革。

公式:

在这里插入图片描述

此中:

  • EMAt = 时间t的指数移动均匀
  • α = 观察权重随时间的低落水平
  • at = 在时间t的观察
[code]# 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) [/code]

输出

[code][1, 1.5, 2.25, 4.62, 6.81] [/code]

方法1:利用Pandas

Python的Pandas模块提供了一种简朴的方法来计算一系列观测值的指数移动均匀值。它提供了一种称为pandas.Series.ewm.mean()的方法,用于计算给定观测值的指数移动均匀值。

pandas.Series.ewm()接受一个称为平滑因子的参数,即观察值的权重随时间减少的水平。平滑因子的值始终介于0和1之间。

[code]# 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) [/code]

输出

[code][1.0, 1.5, 2.25, 4.62, 6.81][/code]

应用场景

  • 时间序列分析:它用于平滑短期厘革并突出恒久观察,如趋势和周期。
  • 金融分析:它用于股票市场的财务分析,如计算股票代价,回报和分析市场趋势。
  • 情况工程:它用于分析情况条件,考虑各种因素,如污染物的浓度等。
  • 计算机性能分析:它通过计算均匀CPU利用率、均匀进程队列长度等指标来分析计算机性能。

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


来源:https://www.jb51.net/python/3287731g6.htm
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

最新评论

关闭

站长推荐上一条 /6 下一条

QQ|手机版|小黑屋|梦想之都-俊月星空 ( 粤ICP备18056059号 )|网站地图

GMT+8, 2025-7-1 19:16 , Processed in 0.037200 second(s), 19 queries .

Powered by Mxzdjyxk! X3.5

© 2001-2025 Discuz! Team.

返回顶部