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,一经查实,立即删除!

相关文章

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

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

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

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

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

机器学习 啤酒数据集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),是…

ER TO SQL语句

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

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

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

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…

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

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

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

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

使用TensorFlow概率预测航空乘客人数

TensorFlow Probability uses structural time series models to conduct time series forecasting. In particular, this library allows for a “scenario analysis” form of modelling — whereby various forecasts regarding the future are made.TensorFlow概率使用结构…

python画激活函数图像

导入必要的库 import math import matplotlib.pyplot as plt import numpy as np import matplotlib as mpl mpl.rcParams[axes.unicode_minus] False 绘制softmax函数图像 fig plt.figure(figsize(6,4)) ax fig.add_subplot(111) x np.linspace(-10,10) y sigmoid(x)ax.s…

pdf.js插件使用记录,在线打开pdf

pdf.js插件使用记录,在线打开pdf 原文:pdf.js插件使用记录,在线打开pdf天记录一个js库:pdf.js。主要是实现在线打开pdf功能。因为项目需求需要能在线查看pdf文档,所以就研究了一下这个控件。 有些人很好奇,在线打开pdf…

程序员 sql面试_非程序员SQL使用指南

程序员 sql面试Today, the word of the moment is DATA, this little combination of 4 letters is transforming how all companies and their employees work, but most people don’t really know how data behaves or how to access it and they also think that this is j…

r a/b 测试_R中的A / B测试

r a/b 测试什么是A / B测试? (What is A/B Testing?) A/B testing is a method used to test whether the response rate is different for two variants of the same feature. For instance, you may want to test whether a specific change to your website lik…

Java基础回顾

内容: 1、Java中的数据类型 2、引用类型的使用 3、IO流及读写文件 4、对象的内存图 5、this的作用及本质 6、匿名对象 1、Java中的数据类型 Java中的数据类型有如下两种: 基本数据类型: 4类8种 byte(1) boolean(1) short(2) char(2) int(4) float(4) l…

计算机部分应用显示模糊,win10系统打开部分软件字体总显示模糊的解决方法-电脑自学网...

win10系统打开部分软件字体总显示模糊的解决方法。方法一:win10软件字体模糊1、首先,在Win10的桌面点击鼠标右键,选择“显示设置”。2、在“显示设置”的界面下方,点击“高级显示设置”。3、在“高级显示设置”的界面中&#xff0…

Tomcat调节

Tomcat默认可以使用的内存为128MB,在较大型的应用项目中,这点内存是不够的,需要调大,并且Tomcat本身不能直接在计算机上运行,需要依赖于硬件基础之上的操作系统和一个java虚拟机。 AD: 这里向大家描述一下如何使用Tom…

turtle 20秒画完小猪佩奇“社会人”

转载:https://blog.csdn.net/csdnsevenn/article/details/80650456 图片源自网络 作者 丁彦军 如需转载,请联系原作者授权。 今年社交平台上最火的带货女王是谁?范冰冰?杨幂?Angelababy?不,是猪…

最佳子集aic选择_AutoML的起源:最佳子集选择

最佳子集aic选择As there is a lot of buzz about AutoML, I decided to write about the original AutoML; step-wise regression and best subset selection. Then I decided to ignore step-wise regression because it is bad and should probably stop being taught. That…

Java虚拟机内存溢出

最近在看周志明的《深入理解Java虚拟机》,虽然刚刚开始看,但是觉得还是一本不错的书。对于和我一样对于JVM了解不深,有志进一步了解的人算是一本不错的书。注明:不是书托,同样是华章出的书,质量要比《深入剖…