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个服务器响应时间中的最后几行。

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.pymovingaverageslist.py
  • movingaverages_test.pymovingaverages_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.taodudu.cc/news/show-3092819.html

相关文章:

  • echarts 自定义平均线值
  • java移动平均线算法_移动平均线的原理是什么? 移动平均线计算公式详解
  • 股票移动平均线matlab,matlab – 计算移动平均线
  • HTML期末学生大作业 基于HTML+CSS+JavaScript通用的后台管理系统ui框架模板
  • web前端期末大作业 基于HTML+CSS+JavaScript学生宿舍管理系统
  • 那些有趣/实用的 Chrome 扩展神器系列(六)
  • 职场干货:身为程序员的你,用了多长时间学习和研究,才达到某一领域技术专家的水平?
  • 【Android+OkHttp3+Jsoup】 模拟登录教务系统 抓取课表和成绩
  • 南京农业大学教务系统大学生抢课——基于python的selenium包+谷歌浏览器
  • 手机网站常用的推广方式有哪些
  • 开发WAP网站入门
  • WAP1.0 前端开发经验(转)
  • 微博推广
  • WAP 调用BREW
  • APP和WAP手机网站的区别
  • WWW和WAP的信息流程有什么区别
  • app和wap手机网站的区别在那里
  • 开通QQ推广方法
  • WAP网站推广
  • WAP网站的推广方式(自整合篇)
  • 关于团队合作
  • 软件测试团队收获,软件测试中如何团队合作才能和谐?
  • 团队协助 开源项目_几款研发团队协作工具对比
  • 解析团队合作关系
  • 团队协作出了问题,项目经理怎么办?
  • 跨团队协作:提高团队生产力的 7 种策略
  • 团队协作的三个基本要素——分工 合作 监督
  • 如何使用Git进行团队协作开发
  • 团队合作的6个原则
  • 关于团队合作的一些看法

python 移动平均线_Python中的移动平均线相关推荐

  1. python 移动平均线_Python 计算EMA(指数移动平均线)

    总结 使用递归和循环两种方法来完成 python环境下循环相比于递归更快,更适应极端样本情况 递归 def _ema(arr,i=None): N = len(arr) α = 2/(N+1) #平滑 ...

  2. python移动平均线绘图_移动平均线绘图

    三线趋势交易法总结趋势线绘制-三线趋势交易法 002350股票,简要说明 移动平均线是技术性分析家们用于绘图的某一证劵(股票)或期货交易以往市场价格的线.目地是用于预知未来的价钱转变.假如恰当立体画出 ...

  3. python 误差线_Python | 绘图中的误差线

    python 误差线 It is one of the most important aspects of plotting. Because of the huge application in e ...

  4. python移动平均线绘图_对python pandas 画移动平均线的方法详解

    数据文件 66001_.txt 内容格式: date,jz0,jz1,jz2,jz3,jz4,jz5 2012-12-28,0.9326,0.8835,1.0289,1.0027,1.1067,1.0 ...

  5. python用电预测_Python中利用长短期记忆模型LSTM进行时间序列预测分析-预测电力消耗数据...

    此示例中,神经网络用于使用2011年4月至2013年2月期间的数据预测都柏林市议会公民办公室的能源消耗. 每日数据是通过总计每天提供的15分钟间隔的消耗量来创建的. LSTM简介 LSTM(或长期短期 ...

  6. python map用法_Python中ChainMap的一种实用用法

    Python部落(python.freelycode.com)组织翻译,禁止转载,欢迎转发. 简而言之ChainMap:将多个字典视为一个,解锁Python超能力. Python标准库中的集合模块包含 ...

  7. python基本统计量_Python中简单统计量的计算

    本篇文章给大家带来的内容是关于Python中简单统计量的计算,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 1.这些操作都要确保已经在电脑中安装好了Anaconda集成库,如果安装好 ...

  8. python解析原理_Python 中 -m 的典型用法、原理解析与发展演变

    在命令行中使用 Python 时,它可以接收大约 20 个选项(option),语法格式如下: python [-bBdEhiIOqsSuvVWx?] [-c command | -m module- ...

  9. python正则表达式空格_python中的正则表达式的使用

    一.正则表达式简介 正则表达式:又称正规表示式.正规表示法.正规表达式.规则表示式.常规表示法(英语:Regular Expression,在代码中常简写为regex.regexp或者是RE),是计算 ...

最新文章

  1. 7 OC 中class 类的结构
  2. 设置centos6.5虚拟机时间同步
  3. 宝塔php open_basedir restriction in effect
  4. 安装docker和jupyter采坑历程
  5. [leetcode] 872. 叶子相似的树(周赛)
  6. 人脸识别门禁系统java实现_java实现人脸识别源码【含测试效果图】——前台显示层(index.jsp)...
  7. Pyppeteer使用代理IP(需要权限验证)
  8. 为什么程序员不应该在同一家公司待太久?
  9. ssm人力资源考勤系统
  10. 运动会分数统计的实验报告(数组实现)
  11. 小心肝队-冲刺日志(第四天)
  12. matlab生成高速轨道不平顺谱,国内外高速铁路轨道不平顺谱对比与思考.pdf
  13. spring boot 快速入门
  14. 【微博技巧】绕开微博绑定手机注册微博
  15. [题集]ADS13 Randomized Algorithms
  16. (十)CMake链接已有的动态库
  17. python 物理公式计算_计算重力/跳跃
  18. Keil 官网下载PACK包的地址
  19. 华容道解法(1)——横刀立马
  20. Java开发者必看避坑指南!人生转折!

热门文章

  1. 计算机等级考试进制转换,计算机等级考试进制转换及常用函数
  2. 数学分析讲义习题解答:(三:第二部分)
  3. 2022-04-14 工作记录--LayUI-数据表格中固定列对应的合计部分也实现固定
  4. 数据结构与算法之Python实现——单链表
  5. 【干货】直播聊天室详细分解,让你一眼学会快速搭建!
  6. C#中的位操作:BitVector32结构
  7. matlab用抛物线族表示马鞍面,matlab实验报告
  8. 高佣次方递增营销联盟模式
  9. NCBI查看基因注释信息
  10. 抓虾 — Bloglines Plus