python 移动平均线_Python中的移动平均线

python 移动平均线

There are situations, particularly when dealing with real-time data, when a conventional average is of little use because it includes old values which are no longer relevant and merely give a misleading impression of the current situation. The solution to this problem is to use moving averages, ie. the average of the most recent values rather than all values, which I will implement in Python.

在某些情况下,尤其是在处理实时数据时,常规平均值很少使用,因为常规平均值包括不再相关的旧值,只会给当前情况带来误导性印象。 解决此问题的方法是使用移动平均值。 我将在Python中实现的最新值而不是所有值的平均值。

To illustrate the problem I will show part of the output of the program I’ll write for this post. It shows the last few rows of a set of 1000 server response times.

为了说明这个问题,我将显示我将为这篇文章编写的程序输出的一部分。 它显示了一组1000个服务器响应时间中的最后几行。

Image for post
The last few rows of a set of 1000 server response times
一组1000个服务器响应时间中的最后几行

Most times in the left hand column are between 10ms and 50ms and can be considered normal but the last few shoot up considerably. The second column shows overall averages which we might use to monitor the server for any problems. However, the large number of normal times included in these averages mean that although the server has slowed to a crawl for the last few requests the averages have hardly risen at all and we wouldn’t realise anything was wrong. The last column shows 4-point moving averages, or the averages of only the last four values. These of course do increase a lot and so alarm bells should start to ring.

左栏中的大多数时间都在10毫秒至50毫秒之间,可以认为是正常的,但最后几次大幅上升。 第二列显示总体平均值,我们可以使用总体平均值来监视服务器是否存在任何问题。 但是,这些平均值中包含大量的正常时间,这意味着尽管服务器在最近的几个请求中已放缓到爬网的速度,但平均值几乎没有上升,我们也不会意识到有什么不妥。 最后一列显示4点移动平均值,或仅显示最后四个值的平均值。 这些当然会增加很多,因此警钟应该开始响起。

Having explained both the problem and its solution let’s write some code. This project consists of the following files which you can clone/download from the Github repository.

解释了问题及其解决方案后,让我们编写一些代码。 该项目包含以下文件,您可以从Github存储库中克隆/下载这些文件。

  • movingaverageslist.py

    movingaverageslist.py
  • movingaverages_test.py

    movingaverages_test.py

The movingaverageslist.py file implements a class which maintains a list of numerical values, and each time a new value is added the overall average and moving average up to that point are also calculated.

movingaverageslist.py文件实现了一个维护数值列表的类,并且每次添加新值时,也将计算总体平均值和直至该点的移动平均值。

In __init__ we simply create an empty list, and set the points attribute, ie. the number of values used to calculate the average.

__init__我们仅创建一个空列表,并设置points属性,即。 用于计算平均值的值的数量。

In the append method, the overall and moving averages are calculated using separate functions which I’ll come to in a minute. Then a dictionary containing the new value and the two averages is appended to the list.

append方法中,总体和移动平均值是使用单独的函数计算的,我将在稍后介绍。 然后,将包含新值和两个平均值的字典添加到列表中。

In __calculate_overall_average we don’t need to add up all the values each time, we can just multiply the previous average by the count and then add the new value. This is then divided by the length + 1, ie. the length the list will be when the new value is added.

__calculate_overall_average我们不需要每次都将所有值相加,只需将先前的平均值乘以计数,然后添加新值即可。 然后将其除以长度+ 1,即。 添加新值时列表的长度。

The __calculate_moving_average function uses a similar technique but is more complex as it has to allow for the list not yet having reached the length of the number of points. In this situation it just calculates the mean of whatever data the list has.

__calculate_moving_average函数使用类似的技术,但更为复杂,因为它必须允许列表尚未达到点数的长度。 在这种情况下,它只计算列表中任何数据的平均值。

Lastly we implement __str__ which returns the data in a table format suitable for outputting to the console.

最后,我们实现了__str__ ,它以适合于输出到控制台的表格格式返回数据。

The MovingAveragesList class is now complete so let’s put together a simple demo.

现在, MovingAveragesList类已经完成,因此让我们进行一个简单的演示。

In main we call populate_response_times to get a MovingAveragesList object with 1000 items, and then print the object. As we implemented __str__ in the class this will be called and therefore we’ll see the table described above.

main函数中,我们调用populate_response_times以获取包含1000个项目的MovingAveragesList对象,然后打印该对象。 当我们在类中实现__str__ ,它将被调用,因此我们将看到上述表格。

I have also added a line which prints the last item in the list just to show how to access the most recent value and averages. A possible enhancement would be to wrap this in a method to avoid rummaging around in the inner workings of the class.

我还添加了一行,用于打印列表中的最后一项,以显示如何访问最新值和平均值。 可能的增强方法是将其包装在一种方法中,以避免在类的内部工作过程中四处乱搞。

The populate_response_times function creates a MovingAveragesList object with a points value of 4. This is probably too low for practical purposes but it does make manual testing easier!

populate_response_times函数创建一个MovingAveragesList对象,其点值为4。这对于实际目的来说可能太低了,但是它确实使手动测试变得更加容易!

It then adds a large number of “normal” values to it; remember that each time a value is added new overall and moving averages are also added. Then a few large numbers are added to simulate a server problem before we return the object.

然后为它添加了大量的“正常”值; 请记住,每次添加值时都会添加新的总体和移动平均值。 然后在我们返回对象之前,添加一些大数字来模拟服务器问题。

Now we can run the program like this…

现在我们可以像这样运行程序了……

python3.8 movingaverages_test.py

python3.8 movingaverages_test.py

I won’t repeat the output but you’ll see 1000 rows of data whizzing up your console.

我不会重复输出,但是您会看到1000行数据在控制台上飞驰。

可能的改进 (Possible Improvements)

The MovingAveragesList class has been tailored to demonstrating the problem it solves and how it does it. In a production environment this are unnecessary and there are a few improvements which could make the class more efficient and useful.

MovingAveragesList类经过定制,以演示其解决的问题以及如何解决此问题。 在生产环境中,这是不必要的,并且有一些改进可以使类更高效,更有用。

  • We could drop the overall averages

    我们可以降低总体平均水平
  • Only the latest moving average could be kept

    只能保留最新的移动平均线
  • We could delete the oldest value each time a new one is added, just keeping a restricted number of the latest values

    每次添加新值时,我们都可以删除最旧的值,而只保留有限数量的最新值
  • We could forget the list concept entirely and just keep a single moving average, updated from any new values added

    我们可能会完全忘记列表概念,而只保留一个移动平均值,并根据添加的任何新值进行更新
  • We could include a threshold and function to be called if the threshold is exceeded, for example sending out emails if the server response time slows to an unacceptable level

    我们可以包括一个阈值和一个超过该阈值的函数,例如,如果服务器响应时间降至不可接受的水平,则发送电子邮件

翻译自: https://medium.com/explorations-in-python/moving-averages-in-python-f72a3249cf07

python 移动平均线

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/388496.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Ireport制作过程

Ireport制作过程 1、首先要到Option下设置一下ClassPath添加文件夹 2、到预览->报表字段设置一下将要用到的字段 3、到编辑->查询报表->写sql语句,然后把语句查询的字段结果与上面设置的报表字段的名要对应上 4、Option->选项->Compiler设置一下…

2018.09.16 loj#10243. 移棋子游戏(博弈论)

传送门 题目中已经给好了sg图&#xff0c;直接在上面跑出sg函数即可。 最后看给定点的sg值异或和是否等于0就判好了。 代码&#xff1a; #include<bits/stdc.h> #define N 2005 #define M 6005 using namespace std; int n,m,k,sg[N],first[N],First[N],du[N],cnt0,an…

html5字体的格式转换,font字体

路由器之家网今天精心准备的是《font字体》&#xff0c;下面是详解&#xff01;html中的标签是什么意思HTML提供了文本样式标记&#xff0c;用来控制网页中文本的字体、字号和颜色&#xff0c;多种多样的文字效果可以使网页变得更加绚丽。其基本语法格式&#xff1a;文本内容fa…

红星美凯龙牵手新潮传媒抢夺社区消费市场

瞄准线下流量红利&#xff0c;红星美凯龙牵手新潮传媒抢夺社区消费市场 中新网1月14日电 2019年1月13日&#xff0c;红星美凯龙和新潮传媒战略合作发布会在北京召开&#xff0c;双方宣布建立全面的战略合作伙伴关系。未来&#xff0c;新潮传媒的梯媒产品将入驻红星美凯龙的全国…

机器学习 啤酒数据集_啤酒数据集上的神经网络

机器学习 啤酒数据集Artificial neural networks (ANNs), usually simply called neural networks (NNs), are computing systems vaguely inspired by the biological neural networks that constitute animal brains.人工神经网络(ANN)通常简称为神经网络(NNs)&#xff0c;是…

实例演示oracle注入获取cmdshell的全过程

以下的演示都是在web上的sql plus执行的&#xff0c;在web注入时 把select SYS.DBMS_EXPORT_EXTENSION.....改成   /xxx.jsp?id1 and 1<>a||(select SYS.DBMS_EXPORT_EXTENSION.....)   的形式即可。(用" a|| "是为了让语句返回true值)   语句有点长…

html视频位置控制器,html5中返回音视频的当前媒体控制器的属性controller

实例检测该视频是否有媒体控制器&#xff1a;myViddocument.getElementById("video1");alert("Controller: " myVid.controller);定义和用法controller 属性返回音视频的当前媒体控制器。默认地&#xff0c;音视频元素不会有媒体控制器。如果规定了媒体控…

ER TO SQL语句

ER TO SQL语句的转换&#xff0c;在数据库设计生命周期的位置如下所示。 一、转换的类别 从ER图转化得到关系数据库中的SQL表&#xff0c;一般可分为3类&#xff1a; 1&#xff09;转化得到的SQL表与原始实体包含相同信息内容。该类转化一般适用于&#xff1a; 二元“多对多”关…

dede 5.7 任意用户重置密码前台

返回了重置的链接&#xff0c;还要把&amp删除了&#xff0c;就可以重置密码了 结果只能改test的密码&#xff0c;进去过后&#xff0c;这个居然是admin的密码&#xff0c;有点头大&#xff0c;感觉这样就没有意思了 我是直接上传的一句话&#xff0c;用菜刀连才有乐趣 OK了…

nasa数据库cm1数据集_获取下一个地理项目的NASA数据

nasa数据库cm1数据集NASA provides an extensive library of data points that they’ve captured over the years from their satellites. These datasets include temperature, precipitation and more. NASA hosts this data on a website where you can search and grab in…

注入代码oracle

--建立类 select SYS.DBMS_EXPORT_EXTENSION.GET_DOMAIN_INDEX_TABLES(FOO,BAR,DBMS_OUTPUT".PUT(:P1);EXECUTE IMMEDIATE DECLARE PRAGMA AUTONOMOUS_TRANSACTION;BEGIN EXECUTE IMMEDIATE  create or replace and compile java source named "LinxUtil" as …

html5包含inc文件,HTML中include file标签的用法

参数PathType将 FileName 的路径类型。路径可为以下某种类型&#xff1a;路径类型 含义文件 该文件名是带有 #include 命令的文档所在目录的相对路径。被包含文件可位于相同目录或子目录中&#xff1b;但它不能处于带有 #include 命令的页的上层目录中。虚拟 文件名为 Web 站点…

r语言处理数据集编码_在强调编码语言或工具之前,请学习这3个基本数据概念

r语言处理数据集编码重点 (Top highlight)I got an Instagram DM the other day that really got me thinking. This person explained that they were a data analyst by trade, and had years of experience. But, they also said that they felt that their technical skill…

springboot微服务 java b2b2c电子商务系统(一)服务的注册与发现(Eureka)

一、spring cloud简介spring cloud 为开发人员提供了快速构建分布式系统的一些工具&#xff0c;包括配置管理、服务发现、断路器、路由、微代理、事件总线、全局锁、决策竞选、分布式会话等等。它运行环境简单&#xff0c;可以在开发人员的电脑上跑。Spring Cloud大型企业分布式…

linux部署服务器常用命令

fdisk -l 查分区硬盘 df -h 查空间硬盘 cd / 进目录 ls/ll 文件列表 vi tt.txt iinsert 插入 shift: 进命令行 wq 保存%退出 cat tt.txt 内容查看 pwd 当期目录信息 mkdir tt建目录 cp tt.txt tt/11.txt 拷贝文件到tt下 mv 11.txt /usr/ 移动 rm -rf tt.txt 删除不提示 rm t…

HTML和CSS面试问题总结,html和css面试总结

html和cssw3c 规范结构化标准语言样式标准语言行为标准语言1) 盒模型常见的盒模型有w3c盒模型(又名标准盒模型)box-sizing:content-box和IE盒模型(又名怪异盒模型)box-sizing:border-box。标准盒子模型&#xff1a;宽度内容的宽度(content) border padding margin低版本IE盒子…

css清除浮动float的七种常用方法总结和兼容性处理

在清除浮动前我们要了解两个重要的定义&#xff1a; 浮动的定义&#xff1a;使元素脱离文档流&#xff0c;按照指定方向发生移动&#xff0c;遇到父级边界或者相邻的浮动元素停了下来。 高度塌陷&#xff1a;浮动元素父元素高度自适应&#xff08;父元素不写高度时&#xff0c;…

数据迁移测试_自动化数据迁移测试

数据迁移测试Data migrations are notoriously difficult to test. They take a long time to run on large datasets. They often involve heavy, inflexible database engines. And they’re only meant to run once, so people think it’s throw-away code, and therefore …

使用while和FOR循环分布打印字符串S='asdfer' 中的每一个元素

方法1&#xff1a; s asdfer for i in s :print(i)方法2:index 0 while 1:print(s[index])index1if index len(s):break 转载于:https://www.cnblogs.com/yuhoucaihong/p/10275800.html

山师计算机专业研究生怎么样,山东师范大学有计算机专业硕士吗?

山东师范大学位于山东省济南市&#xff0c;学校是一所综合性高等师范院校。该院校深受广大报考专业硕士学员的欢迎&#xff0c;因此很多学员想要知道山东师范大学有没有计算机专业硕士&#xff1f;山东师范大学是有计算机专业硕士的。下面就和大家介绍一下培养目标有哪些&#…